您的位置:首页 > 编程语言 > Java开发

leetcode Reverse Integer(Java)

2017-06-11 22:53 281 查看
题目链接:点击打开链接

类型:数学运算

解法:移位运算

public class Solution {
public int reverse(int x) {
long result = 0;
boolean negative = false;

if (x < 0)
{
x = -x;
negative = true;
}

while (x > 0)
{
result = result * 10 + x % 10;
x = x / 10;
}

if (negative)
result = -result;

result = (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE)?0:result;

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