leetcode 7. Reverse Integer

来源:互联网 发布:java多线程运用场景 编辑:程序博客网 时间:2024/06/03 12:37

7. Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123Output:  321

Example 2:

Input: -123Output: -321

Example 3:

Input: 120Output: 21

Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.


class Solution {public:    int reverse(int x)    {           if(x==0 || x==INT_MIN)            return 0;                bool fushu = false;        if (x < 0)        {            fushu = true;             x = -x;        }        vector<int> m1;        while (x != 0)        {            m1.push_back(x % 10);            x = x / 10;        }                    long long sum = m1[0];        int k = 1;        while(k < m1.size())        {            sum = sum * 10 + m1[k];            k++;        }                if (sum > 2147483648)            return 0;                    return fushu ? -sum : sum;    }};



原创粉丝点击