Skip to content

Commit dc72d25

Browse files
author
Yeqi Tao
committed
Add soulution.go for 0007.Reverse Integer
1 parent 857dc09 commit dc72d25

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
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
15+
}
16+
flag = false
17+
result = 10 * result + slot[i]
18+
}
19+
if result > math.MaxInt32 || result < math.MinInt32 {
20+
return 0
21+
}
22+
return result
23+
}

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
├── 0007.Reverse Integer
4848
│   ├── README.md
4949
│   ├── Solution.java
50+
│   ├── Solution.go
5051
│   ├── Solution.js
5152
│   ├── Solution.py
5253
│   ├── Solution.rb

0 commit comments

Comments
 (0)