LeetCode: Reverse Integer

来源:互联网 发布:Mac显示桌面 编辑:程序博客网 时间:2024/04/28 20:59
class Solution {
public:
    int reverse(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        bool flag = true;
        if(x<0){
            flag = false;
            x = -x;
        }
        long int tx = 0;
        while(x){
            tx = 10*tx + x%10;
            x /= 10;
        }
        if(flag){
            if(tx>0x7fffffff)
                tx = 0x7fffffff;
            return tx;
        }
        else{
            tx = -tx;
            if(tx<0x80000000)
                tx = 0x80000000;
            return tx;
        }
    }
};
原创粉丝点击