整数反转 (Reverse Integer)

 

思路:

// @Title: 整数反转 (Reverse Integer)
// @Author: qisiii
// @Date: 2024-01-03 10:12:25
// @Runtime: 0 ms
// @Memory: 39.7 MB
// @comment: 
// @flag: 
class Solution {
   public  static int reverse (int x) {
       if(x==0){
           return x;
       }
        int result=0;
        int temp=x;
        if(x<0){
            temp=Math.abs(x);
        }
        int t=0;
        while(temp>0){
            result=result*10+temp%10;
            if (t!=((result-temp%10)/10)){
                return 0;
            }
            t=result;
            temp=temp/10;
        }
        if(x<0){
            result=result*-1;
        }
        return result;
    }
}

思路:

// @Title: 整数反转 (Reverse Integer)
// @Author: qisiii
// @Date: 2024-04-11 21:12:22
// @Runtime: 0 ms
// @Memory: 39.9 MB
// @comment: 
// @flag: 
class Solution {
    public int reverse(int x) {
        int flag = 1;
        if (x < 0) {
            flag = -1;
            x = Math.abs(x);
        }
        int result = 0, old = 0;
        while (x > 0) {
            int mod = x % 10;
            result = result * 10 + mod;
            if ((result - mod) / 10 != old) {
                return 0;
            }
            old = result;
            x = x / 10;
        }

        return flag * result;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18