2.15 LeetCode整数反转

#标准
class Solution {
    public int reverse(int x) {
        int ans = 0;
        while (x != 0) {
            int pop = x % 10;
            if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && pop > 7)) 
                return 0;
            if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE / 10 && pop < -8)) 
                return 0;
            ans = ans * 10 + pop;
            x /= 10;
        }
        return ans;
    }
}

my

class reverse {

    public static void main(String[] args)
    {
        
		System.out.print(reverse(123));
 
    }

    public static int reverse(int x) {
		int num = 0;
	
		if (x<-Math.pow(2, 31) | x>Math.pow(2, 31)-1 | x==0)
		{
			return 0;
		}
		else
			while(x>0)
			{
				
				int s1 = x%10;
				//s1 = s1*10
				num = num*10+s1;
				x = x/10;
					
			}
		
	return num;			
	}	

}

猜你喜欢

转载自blog.csdn.net/qq_37602161/article/details/113819877
今日推荐