imashutosh51

string to integer

Feb 26th, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. """
  2. 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.
  3. """
  4. class Solution:
  5.     def myAtoi(self, s: str) -> int:
  6.         s=s.lstrip()
  7.         if not s:
  8.             return 0
  9.         sign=1
  10.         sign= -1 if s[0]=='-' else 1
  11.         s=s[1:] if s[0] in '+-' else s[0:]
  12.         ans=0
  13.         print(s)
  14.         for i in s:
  15.             if i.isdigit():
  16.                 print(int(i))
  17.                 ans=ans*10+int(i)
  18.             else:
  19.                 break
  20.        
  21.         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)
  22.  
  23.        
Advertisement
Add Comment
Please, Sign In to add comment