3.27 新做的
class Solution {
public int reverse(int x) {
long sum = 0;
int flag = 1;
if(x < 0)
{
flag = -1;
x = -x;
}
while(x > 0)
{
int temp = x % 10;
System.out.println("x_1:"+ x);
sum = sum * 10 + temp;
System.out.println("sum:"+ sum);
x = x/10;
System.out.println("x_2:"+ x);
}
sum = flag*sum;
if(sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE)
{
return 0;
}
return (int)sum;
}
}
#标准
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;
}
}