7. Reverse Integer(easy)

版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/86529451

 

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

Example 1:

Input: 123

Output:  321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 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.

 

C++:

/*
 @Date    : 2018-01-06 13:26:27
 @Author  : 酸饺子 ([email protected])
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://leetcode.com/problems/reverse-integer/description/
 */

class Solution
{
public:
    int reverse(int x)
    {
        string s = to_string(x);
        std::reverse(s.begin(), s.end());
        long long i = stoll(s);
        if ( i > 2147483647 || i < -2147483648)
            return 0;
        if (x < 0)
            return -i;
        return i;
    }
};

猜你喜欢

转载自blog.csdn.net/SourDumplings/article/details/86529451