LeetCode 7. Reverse Integer

来源:互联网 发布:excel单元格数据合并 编辑:程序博客网 时间:2024/05/29 20:01



Reverse digits of an integer.

Example1:

x = 123, return 321

Example2:

x = -123, return -321


这个题比较简单,就是数字逆序输出。需要注意的是要考虑Overflow的情况,输出为0。

class Solution {public: int reverse(int x) {        int sign = x>=0?1:-1;        int intmax = 2147483647;        int res;        long long int ans=0;        long long int nx = (long long int) x;        nx = abs(nx);        while(nx!=0){            ans=ans*10+nx%10;            nx/=10;        }        if(ans>intmax) res = 0;        else res = ans*sign;        return res;    }};
原创粉丝点击