#=================================== # Tanzila Islam # Email: tanzilamohita@gmail.com # Language: Python 3 #=================================== class Solution: def myAtoi(self, s: str) -> int: s = s.strip() if len(s) == 0: return 0 res = 0 sign = 1 flag = 0 if s[0] == '-': sign = -1 for i in range(len(s)): if s[i].isnumeric(): res = res*10+(ord(s[i])-ord('0')) flag = 1 elif (s[i] == "+" or s[i] == "-") and (flag == 0): flag = 1 pass else: break res = res*sign if res in range((-2**31), (2**31)-1): return res elif res<0: return -2**31 else: return 2**31-1
Wednesday, 19 October 2022
String - 8. String to Integer (atoi)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment