本文给出了Leetcode第7题的代码.

Reverse Integer

Reverse digits of an integer.

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

click to show spoilers.

Note: The input is assumed to be a 32-bit signed integer. Your
function should return 0 when the reversed integer overflows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
public int reverse(int x) {
long res = 0;
int remainder = 0;
while(x != 0){
remainder = x%10;
res = 10*res + remainder;
x /= 10;
}
if(Math.abs(res) > (1<<31) -1) return 0;
return (int)res;
}
}
本文总阅读量次.

留言