首页 > 其他 > 详细

Leetcode-007-整数反转

时间:2020-02-22 15:59:58      阅读:61      评论:0      收藏:0      [点我收藏+]

本题为基础题,注意两个点即可,其一为整数的上下限如何表示,其二是注意 Java 和 Python 的负数整除机制是不同的,Java -3/2 是 -1,而 Python  -3 // 2 是 -2.

class Solution {
    public int reverse(int x) {
        
        long result = 0;
        while(x!=0){
            result = result*10 + x%10;
            x/=10;
        }
        if(result > Integer.MAX_VALUE || result<Integer.MIN_VALUE){
            return 0;
        }
        return (int)result;
    }
}
class Solution:
    def reverse(self, x: int) -> int:
        if(x<0):
            isPos = -1
            x = x*isPos
        else:
            isPos = 1
        result = 0
        while x!=0:
            result = result * 10 + x % 10
            x//=10
        if result>2**31-1 or -result<-2**31:
            return 0
        return isPos*result

 

Leetcode-007-整数反转

原文:https://www.cnblogs.com/huangzengrui/p/12345680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!