数字反转
时间:2020-01-09 20:36:49
收藏:0
阅读:99
给出一个32位有符号的整数,需要将这个整数的数字进行反转输出,若数字超出32位存储范围则输出0;
1 class Solution { 2 public int reverse(int x) { 3 long result=0; 4 while(x!=0){ 5 result=result*10+x%10; 6 x=x/10; 7 } 8 return (result>Integer.MAX_VALUE||result<Integer.MIN_VALUE)?0:(int)result; 9 } 10 }
反思:
1:对Integer.MAX_VALUE和Integer.MIN_VALUE这种表达不熟悉。
2:没有想到定义long类型变量;
评论(0)