-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.go
52 lines (46 loc) · 735 Bytes
/
8.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package string
import (
"math"
"unicode"
)
func MyAtoi(s string) int {
sign := 1
result := 0
i := 0
countSigns := 0
for i < len(s) && !unicode.IsDigit(rune(s[i])) {
char := string(s[i])
if char == "+" {
if countSigns == 1 {
return 0
}
sign = 1
countSigns++
} else if char == "-" {
if countSigns == 1 {
return 0
}
sign = -1
countSigns++
} else if char != " " {
return 0
} else if countSigns == 1 {
return 0
}
i++
}
for i < len(s) && unicode.IsDigit(rune(s[i])) {
char := int(s[i] - '0')
result *= 10
result += char
i++
if result > math.MaxInt32 {
if sign == 1 {
return math.MaxInt32
}
return math.MinInt32
}
}
result *= sign
return result
}