LintCode: Reverse 3-digit Integer

Reverse 3-digit Integer



Similar Problems:


Description
Reverse a 3-digit integer.

You may assume the given number is larger or equal to 100 but smaller than 1000.

Example

Reverse 123 you will get 321.

Reverse 900 you will get 9.

Github: code.dennyzhang.com

Credits To: lintcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// Blog link: https://code.dennyzhang.com/reverse-3-digit-integer
// Basic Ideas:
// Complexity: Time O(1), Space O(1)
/**
 * @param number: A 3-digit number.
 * @return: Reversed number.
 */
func reverseInteger (number int) int {
    res := 0
    for number != 0 {
        res = 10*res+number%10
        number = number/10
    }
    return res
}
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.