-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStringToIntegerAtoi.go
67 lines (58 loc) · 1.29 KB
/
StringToIntegerAtoi.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package StringToIntegerAtoi
//Implement atoi to convert a string to an integer.
//
//Hint: Carefully consider all possible input cases. If you want a challenge,
//please do not see below and ask yourself what are the possible input cases.
//
//Notes: It is intended for this problem to be specified vaguely (ie, no given input specs).
//You are responsible to gather all the input requirements up front.
//
//Accepted.
import (
"strconv"
"strings"
)
const intMaxValue = 2147483647
const intMinValue = -2147483648
func myAtoi(str string) int {
s := strings.Trim(str, " ")
stringLength := len(s)
if stringLength == 0 {
return 0
}
if stringLength == 1 {
if s[0] <= '0' || s[0] >= '9' {
return 0
}
r, _ := strconv.Atoi(s)
return r
}
plus, minus := s[0] == '+', s[0] == '-'
result, startIndex := 0, 0
if plus || minus {
startIndex = 1
}
for i := startIndex; i < stringLength; i++ {
if s[i] >= '0' && s[i] <= '9' {
if intMaxValue/10-int(s[i]-'0') <= result {
if minus && result*10+int(s[i]-'0') == intMaxValue {
return -intMaxValue
}
if minus {
return intMinValue
}
return intMaxValue
}
result = result*10 + int(s[i]-'0')
} else {
if minus {
return -result
}
return result
}
}
if minus {
return -result
}
return result
}