Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
Subscribe to see which companies asked this question.
反转一个整数。
public class Solution {
public int reverse(int x) {
long a = 0;
while(x!=0){
a = a*10 + x%10;
x = x/10;
}
if(a>Integer.MAX_VALUE||a<Integer.MIN_VALUE){
return 0;
}
return (int)a;
}
}
本文介绍了一个简单的整数反转算法,该算法通过循环处理输入的32位带符号整数,并在溢出时返回0。文章提供了完整的Java实现代码。
1518

被折叠的 条评论
为什么被折叠?



