Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def atoi(self, str):
- """
- :type str: str
- :rtype: int
- """
- i = 0
- result = 0
- length_of_number=0
- numberarray=[]
- intMAX=2**31 -1
- intnegMAX=2**31
- negative = False
- while i <= len(str):
- if i == len(str):
- return 0
- if str[i] == '+' or str[i] == '-':
- if str[i] == '-':
- negative = True
- i += 1
- break
- else:
- if str[i] == ' ':
- i += 1
- else:
- break
- while i <= len(str):
- if i == len(str):
- return 0
- if str[i] == '0':
- i += 1
- else:
- break
- while i <= len(str):
- if i == len(str):
- break
- if str[i] in ['0','1', '2', '3', '4', '5', '6', '7', '8', '9']:
- length_of_number += 1
- numberarray.append(int(str[i]))
- i += 1
- else:
- break
- for nu in numberarray:
- result += nu*10**(length_of_number-1)
- length_of_number-=1
- if negative:
- if result <= intnegMAX:
- result*=-1
- else:
- result = intnegMAX
- result*=-1
- else:
- if result > intMAX:
- result = intMAX
- return result
Add Comment
Please, Sign In to add comment