leetcode-#7 Reverse Integer

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 store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

int reverse(int x){
    long dstX = 0;
    int temp = 0;
    
    while (x != 0)
    {
        temp = x % 10;
        x = x / 10;
        dstX = dstX * 10 + temp;
        
        if( (dstX > 0x7fffffff) || (dstX < (signed int)0x80000000))
        {
            return 0;
        }
    }
    
    return dstX;
}
发布了223 篇原创文章 · 获赞 160 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/u014100559/article/details/101072177