-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringToInt.py
More file actions
27 lines (26 loc) · 828 Bytes
/
Copy pathstringToInt.py
File metadata and controls
27 lines (26 loc) · 828 Bytes
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
def stringToInt(str):
# deal with blank space, sign and overflow
index = 0;
res = 0;
sign = 1;
if len(str) == 0:
return 0;
while index< len(str) and str[index] == ' ':
index +=1;
if str[index] == '+' or str[index] == '-':
sign = 1 - 2*(str[index] == '-');
index +=1;
while index< len(str) and ord(str[index])>= ord('0') and ord(str[index])<= ord('9'):
temp = int(str[index]);
if res> 214748364 or (res == 214748364 and temp>7 ):
if sign == 1:
print 2147483647;
return 2147483647;
if sign == -1:
print -2147483648;
return -2147483648;
res = res*10 + temp;
index +=1;
#print sign*res;
return sign*res;
stringToInt('-2147483649');