Given a 32-bit signed integer, reverse digits of an integer.
Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, assume that your function returns 0 when the reversed integer overflows.
Example 1:
1 | Input: x = 123 |
Example 2:
1 | Input: x = -123 |
Example 3:
1 | Input: x = 120 |
Example 4:
1 | Input: x = 0 |
Constraints:
1 | -2ˆ31 <= x <= 231 - 1 |
Java
x /= 10;
x = 123456789;
x = 12345678;
…
x % 10;
9
8
…
reverse = reverse * 10 + x % 10;
987
…
(int)reverse==reverse?(int)reverse:0;
check integer overflow -2,147,483,647 to 2,147,483,647
1 | import java.util.*; |
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)