大三了,快实习了,刷一些题保持手感,毕竟工程做多了,有时候真的很容易遗忘一些算法知识,先刷一些水题,嗯。
Problem:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Solution:
public class Solution {
public int reverse(int x) {
int sign=x<0?-1:1;
x=Math.abs(x);
long r=0;
for ( ; x>0; x=x/10) {
r= r*10 + x%10;
if (r>Integer.MAX_VALUE)
return 0;
}
return (int)r*sign;
}
}