To prove safety Offer 67

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def StrToInt(self, s):
 4         n = len(s)
 5         if n == 0:
 6             return 0
 7         isNegative =  False
 8         if s[0] == '-':
 9             isNegative = True
10         ret = 0
11         for i in range(n):
12             c = s[i]
13             if i == 0 and (c == '+' or c == '-'):
14                 continue
15             if c < '0' or c >'9':
16                 return 0
17             ret = ret * 10 + (ord(c) - ord('0'))
18         return -ret if isNegative else ret
19         # write code here

 

Guess you like

Origin www.cnblogs.com/asenyang/p/11025328.html