Leetcode||7. Reverse Integer

注意溢出情况返回0。

class Solution {
public:
    int reverse(int x) {
        const int max=0x7fffffff;
        const int min=0x80000000;
        long long res = 0;  
        while(x)  
        {  
            res = res*10 + x%10;  
            if(res>max||res<min)
                return 0;
            x /= 10;  
        }  
        return (int)res;  
    }
};

猜你喜欢

转载自blog.csdn.net/sinat_35205772/article/details/52674216