Example1: x = 123, return 321
Example2: x = -123, return -321
Example2: x = -123, return -321
注意:overflow and underflow
Java Code:
public int reverse(int x) { long reversedInt = 0; while(x!=0){ reversedInt = reversedInt * 10 + x%10; x = x/10; if(reversedInt >= Integer.MAX_VALUE) return Integer.MAX_VALUE; if(reversedInt <= Integer.MIN_VALUE) return Integer.MIN_VALUE; } return (int) reversedInt; }
No comments:
Post a Comment