[LintCode] 37. Reverse 3-digit Integer

题目:

Reverse a 3-digit integer.

Example

Reverse 123 you will get 321.

Reverse 900 you will get 9.

Code:

class Solution {
public:
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    int reverseInteger(int number) {
        int size = 0;
        int count_number = number;
        while (count_number / 10 != 0) {
            count_number = count_number / 10;
            size++;
        }
        size++;
        
        int res = 0;
        int temp = 0;
        int count_size = size-1;
        int dig = 1;
        int temp_number = number;
        for (int i = 0;i < size - 1; i++) {
            temp = temp_number % 10;
            temp_number = temp_number / 10;
            dig = 1;
            for (int j = 0; j < count_size; j++) {
                dig = dig * 10;
            }
            count_size--;
            res = res + temp * dig;
        }
        res = res + temp_number;
        return res;
    }
};

你可能感兴趣的:([LintCode] 37. Reverse 3-digit Integer)