Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Clamp the result to the 32-bit signed integer range [−2^31, 2^31 − 1] because the problem restricts outputs to this limit. Even though Python supports arbitrary-size integers, we manually enforce overflow boundaries to match standard integer constraints.
- """
- class Solution:
- def myAtoi(self, s: str) -> int:
- s=s.lstrip()
- if not s:
- return 0
- sign=1
- sign= -1 if s[0]=='-' else 1
- s=s[1:] if s[0] in '+-' else s[0:]
- ans=0
- print(s)
- for i in s:
- if i.isdigit():
- print(int(i))
- ans=ans*10+int(i)
- else:
- break
- return max(-pow(2,31),min(pow(2,31)-1,sign*ans)) # a big positive number will come down in range by min(pow(2,31-2,sign*ans)) and a big negative number will come in range by max(-pow(2,31),ans)
Advertisement
Add Comment
Please, Sign In to add comment