Leetcode 7整数反转

链接:Here

class Solution {
    public int reverse(int x) {
        boolean isN = false;
        
        if(x < 0)
        {
            isN = true;
            x = -x;
        }
        long sum = 0;
        
        while(x!=0){
            sum = sum * 10 + x%10;
            x = x / 10;
        }
        
        if(isN){
            sum = -sum;
        }
        
         if (sum >Integer.MAX_VALUE || sum < Integer.MIN_VALUE)
            return 0;
        return new Long(sum).intValue();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43894577/article/details/88931621