您的位置:首页 > 其它

【leetcode】 Reverse Integer

2015-03-16 23:02 351 查看
问题描述:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321


问题考虑:

1.考虑逆转越界的问题

其余思路常规题解法~

public int reverse(int x) {
int temp=0;
int sign=1;
if(x==Integer.MIN_VALUE)
return 0;
if(x<0){
x=-x;
sign=-1;
}
while(x!=0){
if(temp>Integer.MAX_VALUE/10 )
return 0;
temp=temp*10+x%10;
x=x/10;
//注意这里不能写成 temp*10>Integer.MAX_VALUE,不然temp*10也越界了;

}
return temp*sign;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: