Skip to content

Commit 2e275e3

Browse files
author
Yeqi Tao
committed
Add soulution.go for 0008.String to Integer
1 parent dc72d25 commit 2e275e3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func myAtoi(str string) int {
2+
cer := 0
3+
result := 0
4+
tmpResult := 0
5+
flag := false
6+
for _, n := range str {
7+
if !flag && n == ' ' {
8+
continue
9+
}
10+
flag = true
11+
if cer == 0 {
12+
if n >= '0' && n <= '9' {
13+
cer = 1
14+
} else if n == '+' {
15+
cer = 1
16+
continue
17+
} else if cer == 0 && (n == '-') {
18+
cer = -1
19+
continue
20+
}
21+
}
22+
23+
if n >= '0' && n <= '9' {
24+
tmpResult = tmpResult * 10 + ((int)(n) - 48)
25+
result = cer * tmpResult
26+
} else {
27+
break
28+
}
29+
if result < math.MinInt32 {
30+
return math.MinInt32
31+
}
32+
if result > math.MaxInt32 {
33+
return math.MaxInt32
34+
}
35+
}
36+
return result
37+
}

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
│   └── Solution2.py
5555
├── 0008.String to Integer (atoi)
5656
│   ├── Solution.java
57+
│   ├── Solution.go
5758
│   ├── Solution.js
5859
│   └── Solution.py
5960
├── 0009.Palindrome Number

0 commit comments

Comments
 (0)