Skip to content

Commit 9937c31

Browse files
authored
feat: update solutions to lc problem: No.0007 (doocs#870)
No.0007.Reverse Integer
1 parent 469f340 commit 9937c31

File tree

4 files changed

+48
-70
lines changed

4 files changed

+48
-70
lines changed

solution/0000-0099/0007.Reverse Integer/README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:数学**
57+
58+
设返回结果初始值 $ans = 0$,每次循环对于给定的数 $x$,若 $x \ne 0$,求出个位数字 $digit = x \ \% \ 10$,根据题意当 $ans \times 10 + digit > INT32\\_MAX$ 或者 $ans \times 10 + digit < INT32\\_MIN$ 时返回 $0$,否则我们令 $ans = ans \times 10 + digit$,然后结束本次循环,同时更新 $x = x/10$。
59+
60+
但题目要求不允许存储 64 位整数,故其中的判断条件需要修改为当 $ans > (INT32\\_MAX-digit)/10$ 或者 $ans < (INT32\\_MIN-digit)/10$ 时返回 $0$。
61+
62+
更进一步来看,在 $x > 0$ 时,若 $x$ 的位数小于 $INT32\\_MAX$ 的位数则一定可以反转数字;若 $x$ 的位数等于 $INT32\\_MAX$ 的位数,能否反转则取决于 $x$ 的最高位数字 $digit$,而 $x \leq INT32\\_MAX$,故最高位数字 $digit \leq 2 < INT32\\_MAX \\% 10$,所以判断条件可以简化为 $ans > INT32\\_MAX/10$。同理,当 $x < 0$ 时,判断条件可以简化为 $ans < INT32\\_MIN/10$。
63+
64+
时间复杂度 $O(log|x|)$,空间复杂度 $O(1)$,其中 $log|x|$ 表示有符号整数 $x$ 的位数。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
@@ -93,12 +103,13 @@ class Solution {
93103
class Solution {
94104
public:
95105
int reverse(int x) {
96-
long long ans = 0;
97-
while (x) {
106+
int ans = 0;
107+
for (; x != 0; x /= 10) {
108+
if (ans > INT32_MAX / 10 || ans < INT32_MIN / 10)
109+
return 0;
98110
ans = ans * 10 + x % 10;
99-
x /= 10;
100111
}
101-
return ans < INT_MIN || ans > INT_MAX ? 0 : ans;
112+
return ans;
102113
}
103114
};
104115
```
@@ -161,27 +172,14 @@ impl Solution {
161172

162173
```go
163174
func reverse(x int) int {
164-
slot := make([]int, 11)
165-
count := 0
166-
for x != 0 {
167-
n := x % 10
168-
slot[count] = n
169-
count++
170-
x /= 10
171-
}
172-
result := 0
173-
flag := true
174-
for i := 0; i < count; i++ {
175-
if flag && slot[i] == 0 {
176-
continue
175+
ans, INT32_MAX, INT32_MIN := 0, math.MaxInt32, math.MinInt32
176+
for ; x != 0; x /= 10 {
177+
if ans > INT32_MAX/10 || ans < INT32_MIN/10 {
178+
return 0
177179
}
178-
flag = false
179-
result = 10*result + slot[i]
180-
}
181-
if result > math.MaxInt32 || result < math.MinInt32 {
182-
return 0
180+
ans = ans*10 + x % 10
183181
}
184-
return result
182+
return ans
185183
}
186184
```
187185

solution/0000-0099/0007.Reverse Integer/README_EN.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
## Solutions
4141

42+
**Approach 1: Pop and Push Digits & Check before Overflow**
43+
44+
Time complexity $O(log|x|)$, Space complexity $O(1)$.
45+
4246
<!-- tabs:start -->
4347

4448
### **Python3**
@@ -72,12 +76,13 @@ class Solution {
7276
class Solution {
7377
public:
7478
int reverse(int x) {
75-
long long ans = 0;
76-
while (x) {
79+
int ans = 0;
80+
for (; x != 0; x /= 10) {
81+
if (ans > INT32_MAX / 10 || ans < INT32_MIN / 10)
82+
return 0;
7783
ans = ans * 10 + x % 10;
78-
x /= 10;
7984
}
80-
return ans < INT_MIN || ans > INT_MAX ? 0 : ans;
85+
return ans;
8186
}
8287
};
8388
```
@@ -140,27 +145,14 @@ impl Solution {
140145

141146
```go
142147
func reverse(x int) int {
143-
slot := make([]int, 11)
144-
count := 0
145-
for x != 0 {
146-
n := x % 10
147-
slot[count] = n
148-
count++
149-
x /= 10
150-
}
151-
result := 0
152-
flag := true
153-
for i := 0; i < count; i++ {
154-
if flag && slot[i] == 0 {
155-
continue
148+
ans, INT32_MAX, INT32_MIN := 0, math.MaxInt32, math.MinInt32
149+
for ; x != 0; x /= 10 {
150+
if ans > INT32_MAX/10 || ans < INT32_MIN/10 {
151+
return 0
156152
}
157-
flag = false
158-
result = 10*result + slot[i]
159-
}
160-
if result > math.MaxInt32 || result < math.MinInt32 {
161-
return 0
153+
ans = ans*10 + x % 10
162154
}
163-
return result
155+
return ans
164156
}
165157
```
166158

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution {
22
public:
33
int reverse(int x) {
4-
long long ans = 0;
5-
while (x) {
4+
int ans = 0;
5+
for (; x != 0; x /= 10) {
6+
if (ans > INT32_MAX / 10 || ans < INT32_MIN / 10)
7+
return 0;
68
ans = ans * 10 + x % 10;
7-
x /= 10;
89
}
9-
return ans < INT_MIN || ans > INT_MAX ? 0 : ans;
10+
return ans;
1011
}
11-
};
12+
};
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
func reverse(x int) int {
2-
slot := make([]int, 11)
3-
count := 0
4-
for x != 0 {
5-
n := x % 10
6-
slot[count] = n
7-
count++
8-
x /= 10
9-
}
10-
result := 0
11-
flag := true
12-
for i := 0; i < count; i++ {
13-
if flag && slot[i] == 0 {
14-
continue
2+
ans, INT32_MAX, INT32_MIN := 0, math.MaxInt32, math.MinInt32
3+
for ; x != 0; x /= 10 {
4+
if ans > INT32_MAX/10 || ans < INT32_MIN/10 {
5+
return 0
156
}
16-
flag = false
17-
result = 10*result + slot[i]
18-
}
19-
if result > math.MaxInt32 || result < math.MinInt32 {
20-
return 0
7+
ans = ans*10 + x%10
218
}
22-
return result
9+
return ans
2310
}

0 commit comments

Comments
 (0)