File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
0008.String to Integer (atoi) Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 54
54
│ └── Solution2.py
55
55
├── 0008.String to Integer (atoi)
56
56
│ ├── Solution.java
57
+ │ ├── Solution.go
57
58
│ ├── Solution.js
58
59
│ └── Solution.py
59
60
├── 0009.Palindrome Number
You can’t perform that action at this time.
0 commit comments