LeetCode: Reverse Integer

Title : 

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

主要注意的问题是防止数组越界的问题。还有通过除法、取模计算的要记住

class Solution {
public:
    int reverse(int x) {
        long long result = 0;
        while(x != 0){
            result = result * 10 + x % 10;
            if (result < INT_MIN || result > INT_MAX )
                return 0;
            x = x / 10;
        }
        return result;
    }
};

 

posted on 2015-04-11 11:01  月下之风  阅读(154)  评论(0编辑  收藏  举报

导航