-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringtoInteger.py
More file actions
41 lines (39 loc) · 1.07 KB
/
Copy pathStringtoInteger.py
File metadata and controls
41 lines (39 loc) · 1.07 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
# coding: utf-8
"""
Implement atoi to convert a string to an integer.
整形范围:INT_MAX (2147483647) or INT_MIN (-2147483648)
leetcode python编译器的sys.maxint不是2147483647
"""
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
maxint = 2147483647
if str == None or len(str) < 1:
return 0
temp_str = str.strip()
symbol = True
if temp_str[0] == '-':
symbol = False
i = 1
elif temp_str[0] == '+':
i = 1
else:
i = 0
result = 0
while i < len(temp_str) and temp_str[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
result = result*10 + int(temp_str[i])
if result > maxint:
break
i += 1
if symbol:
if result > maxint:
result = maxint
else:
if result > maxint+1:
result = -(maxint+1)
else:
result = -result
return result