您的位置:首页 > 其它

Leetcode Reverse Integer

2016-07-06 00:54 369 查看
Reverse digits of an integer.

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

Difficulty: Easy

public class Solution {
public int reverse(int x) {
boolean isNeg = false;
if(x < 0) isNeg = true;
x = Math.abs(x);
double ans = 0;
while(x != 0){
ans = ans*10 + x%10;
x = x/10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE)
return 0;
if(isNeg) return -(int)(ans);
return (int)(ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: