Given a 32-bit signed integer, reverse digits of an integer.
Example:
Input: 123
Output: 321
Example:
Input: -123
Output: -321
Example:
Input: 120
Output: 21
代码如下:
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub } public static int reverse(int x){ int result = 0; while(x!=0){ int tail = x%10; int newResult = result*10+tail; if((newResult-tail)/10!=result){ return 0; } result = newResult; x = x/10; } return result; } }