1.如何反转

public static int reverse(int x) {
       //用数学思维来看
        int rpc = 0;
        while (x != 0)
        {
            int newrpc = rpc*10 + x%10;//数学思维取反过程
            rpc = newrpc;
            if ((newrpc - x%10)/10 != rpc) {return 0;}//判断,不相等则说明溢出了
            x = x/10;
           
        }
        return rpc;
    }
完整的反转整数
long temp=0;
        while(x!=0){
            temp*=10;
            temp+=x%10;
            x/=10;
        }
2.反转溢出问题
(1)我觉得溢出这个最简单的解决办法就是捕获异常,返回0
(2)2的31次方,Math.pow(2,31)
(3)为了解决溢出问题,我们使用了long类型来存储结果,最后判断一次即可
posted on 2019-08-26 17:27  慢漫长路  阅读(127)  评论(0编辑  收藏  举报