-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.py
More file actions
57 lines (47 loc) · 1.15 KB
/
atoi.py
File metadata and controls
57 lines (47 loc) · 1.15 KB
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
#!/usr/bin/env python
# encoding: utf8
# String to Integer (atoi): Implement atoi to convert a string to an integer.
__author__ = "Zhu Xianfeng <xianfeng.zhu@gmail.com>"
class Solution:
# @return an integer
def atoi(self, s):
INT_MAX = 2 ** 31 - 1
INT_MIN = -(INT_MAX + 1)
slen = len(s)
if slen == 0:
return 0
# Ignore blank
i = 0
while s[i] in (' ', '\t', '\n', '\0'):
i += 1
if i != 0:
s = s[i:]
mlen = str(INT_MIN)
slen = len(s)
if slen > mlen or slen == 0:
return 0
# Handle flag
flag = 1
if s[0] == '+':
s = s[1:]
elif s[0] == '-':
flag = -1
s = s[1:]
n = 0
for i in s:
if i not in "0123456789":
break
i = int(i)
n = n * 10 + i
n = (n * flag)
if n > INT_MAX:
n = INT_MAX
elif n < INT_MIN:
n = INT_MIN
return n
def main():
s = "2147483648"
sol = Solution()
print sol.atoi(s)
if __name__ == "__main__":
main()