数字反转

给出一个32位有符号的整数,须要将这个整数的数字进行反转输出,若数字超出32位存储范围则输出0;spa

 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 }

反思:code

1:对Integer.MAX_VALUE和Integer.MIN_VALUE这种表达不熟悉。blog

2:没有想到定义long类型变量;io

相关文章
相关标签/搜索