[leetcode刷题系列]Reverse Integer

来源:互联网 发布:枪神纪免费刷英雄软件 编辑:程序博客网 时间:2024/06/05 18:34

这题没啥好说的。直接上代码。不过这题有些不严谨。有些int被reverse之后就超过int了。需要用longlong。

class Solution {public:    int reverse(int x) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        bool flag = false;        if(x < 0)            flag = true, x = -x;        int now = 0;        while(x > 0)            now = now * 10 + x  % 10, x /= 10;        if(flag)            return -now;        else            return now;    }};


原创粉丝点击