Given a 32-bit signed integer, reverse digits of an integer.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) { return0; }
boolean isNe = x < 0 ? true : false; x = Math.abs(x); // 取绝对值时越界了,直接返回0 if (isNe && x < 0) { return0; } StringBuilder sb = new StringBuilder(); long m = 10; long base = 1;
while (true) { base = m * base; long re = x % base; if (base == 10) { sb.append(re); } else { sb.append((re * m) / base); } if (x < base) { break; } } Long res; if (isNe) { res = 0 - Long.parseLong(sb.toString()); } else { res = Long.parseLong(sb.toString()); }
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) { return0; } else { return res.intValue(); } }
publicintreverse1(int x){ int rev = 0; while (x != 0) { int pop = x % 10; x /= 10; // Integer.MAX_VALUE = 2147483647,因为后面 rev = rev * 10 + pop,所以rev >Integer.MAX_VALUE 溢出 // rev == Integer.MAX_VALUE / 10 时,Integer.MAX_VALUE / 10 = 2147483640,so,pop > 7时溢出 if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return0; // Integer.MIN_VALUE = -2147483648 // 同理如上 if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return0; rev = rev * 10 + pop; } return rev; }
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ‘ ‘ is considered as whitespace character. Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 1) or INT_MIN (−2^31) is returned.
Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42. Example 3:
Input: "4193 with words" Output: 4193 Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. Example 4:
Input: "words and 987" Output: 0 Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed. Example 5:
Input: "-91283472332" Output: -2147483648 Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−2^31) is returned.