Reverse Integer, leetcodereverse
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x =-123, return-321
Note:
The input is assumed to be a 32-bit signed integer. Your function shocould return 0 when the reversed integer overflows.
Returns the integer of a number.
Example 1: x = 123, 321 is returned
Example 2: x =-123, returns-321
Note:
Assume that the input is a 32-bit signed integer. When the inverted integer overflows, your function should return 0.
My personal biggest disadvantage of LeetCode is that I can do it as soon as I come up. because English is easy to read without Chinese, many prompts are not noticed at all. If this question overflows, 0 is returned.
class Solution { public int reverse(int x) { boolean isAbove=true; StringBuffer sBuffer=new StringBuffer(); int newX=x; if (x < 0) { isAbove = false; x = Math.abs(x); } while (x > 0) { sBuffer.append(x % 10); x = x / 10; } if (newX!=0) { try { if (isAbove) return Integer.parseInt(sBuffer.toString()); else return -Integer.parseInt(sBuffer.toString()); } catch (Exception e) { return 0; } } else return 0; }}
Next, let's review the value range of the Basic JAVA data types:
The value range of byte is-128 ~ 127, occupies 1 byte (to the power of-2 to the power of 2 to the power of-1)
The short value range is-32768 ~ 32767, occupies 2 bytes (the 15th power of-2 to the 15th power of 2-1)
The value range of int Is (-2147483648 ~ 2147483647), occupies 4 bytes (the 31 Power of-2 to the 31 power of 2-1)
The value range of long is (-9223372036854774808 ~ 9223372036854774807), occupies 8 bytes (63 power of-2 to 63 power of 2-1)