You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a 32-bit signed integer, reverse digits of an integer.
6
+
7
+
**Example 1:**
8
+
9
+
```
10
+
Input: 123
11
+
Output: 321
12
+
```
13
+
14
+
**Example 2:**
15
+
16
+
```
17
+
Input: -123
18
+
Output: -321
19
+
```
20
+
21
+
**Example 3:**
22
+
23
+
```
24
+
Input: 120
25
+
Output: 21
26
+
```
27
+
28
+
**Note:**
29
+
30
+
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
31
+
32
+
## 思路
33
+
使用长整型保存结果。依次模 10 得到最右边一位。
34
+
35
+
## [完整代码][src]
36
+
37
+
```java
38
+
classSolution {
39
+
publicintreverse(intx) {
40
+
long result =0;
41
+
for (; x !=0; x /=10) {
42
+
result = result *10+ x %10;
43
+
}
44
+
if (result >Integer.MAX_VALUE|| result <Integer.MIN_VALUE) {
0 commit comments