forked from ScarlettCC/AlgorithmByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrToInt.py
More file actions
35 lines (34 loc) · 924 Bytes
/
StrToInt.py
File metadata and controls
35 lines (34 loc) · 924 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
28
29
30
31
32
33
34
# -*- coding:utf-8 -*-
class Solution:
def StrToInt(self, s):
# write code here
flag = False
if s==None or len(s)<1:
return 0
numstack = []
dict = {'0': 0, "1": 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
for i in s :
if i in dict.keys():
numstack.append(dict[i])
elif '+' == i:
continue
elif '-' == i:
continue
else:
return 0
if 1 == len(s) and '0'==s[0]:
flag =True
return 0
ans = 0
if 1== len(s) and ('+'==s[0] or '-'==s[0]):
return 0
for i in numstack:
ans = ans*10 + i
if '-' == s[0]:
ans = 0-ans
return ans
s=Solution()
print s.StrToInt('-111')
print s.StrToInt('+')
print s.StrToInt('0')
print s.StrToInt('')