public class Solution {
public static int reverse(int x){
long newNum=0,old=x;//*Notice* long,so that it can compare with MAX_Value
x=(x>0)?x:(0-x);
while(x!=0){
newNum*=10;
newNum+=x%10;
x/=10;
if(newNum>Integer.MAX_VALUE||newNum<Integer.MIN_VALUE)
return 0;
}
if(old>0)
return (int)newNum;
else if(old<0)
return (int)(0-newNum);
else
return 0;
}
}
【LeetCode】Reverse Integer
最新推荐文章于 2021-01-12 00:40:21 发布