07.整数反转
题目:
提交:01
1 class Solution { 2 3 public static int getRev(int x){ 4 int temp =0; 5 while(x!=0){ 6 temp = temp*10+x%10; 7 x = x/10; 8 9 // if((Integer.MAX_VALUE-temp)/10<temp){ 10 // temp = 0; 11 // break; 12 // } 13 } 14 return temp; 15 } 16 17 public static int getFu(int x){ 18 x= -1*x; 19 return -1*getRev(x); 20 } 21 22 23 public int reverse(int x) { 24 int result =0; 25 if(x>=0){ 26 result = getRev(x); 27 }else{ 28 result = getFu(x); 29 } 30 return result; 31 } 32 }
我的困惑:正数/负数 极大值/极小值的处理很棘手
提交02:我想复杂了,有点 sbl
1 class Solution { 2 3 public int reverse(int x) { 4 int max = 0x7fffffff, min = 0x80000000;//int的最大值最小值 5 long temp=0; 6 while(x!=0){ 7 temp=temp*10+x%10; 8 x/=10; 9 } 10 11 if(temp>max||temp<min){ 12 return 0; 13 } 14 else{ 15 return (int)temp; 16 } 17 } 18 }