Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).

The algorithm for myAtoi(string s) is as follows:


	Read in and ignore any leading whitespace.
	Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
	Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
	Convert these digits into an integer (i.e. "123" -&gt; 123, "0032" -&gt; 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
	If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
	Return the integer as the final result.


Note:


	Only the space character ' ' is considered a whitespace character.
	Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.


&nbsp;
Example 1:

Input: s = "42"
Output: 42
Explanation: The underlined characters are what is read in, the caret is the current reader position.
Step 1: "42" (no characters read because there is no leading whitespace)
         ^
Step 2: "42" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "42" ("42" is read in)
           ^
The parsed integer is 42.
Since 42 is in the range [-231, 231 - 1], the final result is 42.


Example 2:

Input: s = "   -42"
Output: -42
Explanation:
Step 1: "   -42" (leading whitespace is read and ignored)
            ^
Step 2: "   -42" ('-' is read, so the result should be negative)
             ^
Step 3: "   -42" ("42" is read in)
               ^
The parsed integer is -42.
Since -42 is in the range [-231, 231 - 1], the final result is -42.


Example 3:

Input: s = "4193 with words"
Output: 4193
Explanation:
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
         ^
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
         ^
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
             ^
The parsed integer is 4193.
Since 4193 is in the range [-231, 231 - 1], the final result is 4193.


&nbsp;
Constraints:


	0 &lt;= s.length &lt;= 200
	s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.


class Solution:

    def assign_sign(self, sign):
      # verify that we haven't already got a sign
      #&nbsp;"+42-" -> we don't want to return -42; hence check
      if not self.is_neg and not self.is_pos:
          # no sign has been set yet
        if sign=="+":
          self.is_pos = True
        elif sign=="-":
          self.is_neg = True
      return

    def add_to_int(self, num):
      if not self.num:
        self.num = num
      else:
        self.num = (self.num*10) + num

    def myAtoi(self, s: str) -> int:
        #&nbsp;remove the leading and trailing spaces
        self.is_neg = False
        self.is_pos = False
        self.num = None
        s=s.strip()
        for i in s:
          # ignore the rest of the string if a non digit character is read
          if i in ("+","-"):
            #&nbsp;only read the first symbol; break if second symbol is read
            if self.is_pos or self.is_neg or isinstance(self.num, int):
              #&nbsp;one of the two symbols is read or a number is read
              break
            self.assign_sign(i)
            continue
          try:
            i = int(i)
            self.add_to_int(i)
          except ValueError:
            # it's neither a sign, nor a number; terminate
            break

        # outside the loop; compile the result
        if not self.num:
          return 0
        upper_limit = 2**31 - 1
        if self.is_pos or (not self.is_pos and not self.is_neg):
          if self.num > upper_limit:
            self.num = upper_limit
        elif self.is_neg:
          if self.num > upper_limit+1:
            self.num = upper_limit+1
          self.num = -1 * self.num
        return self.num
class Solution {
    public int myAtoi(String s) {
        long n=0;
        int i=0,a=0;
        s=s.trim();
        if(s.length()==0)
            return 0;
        if(s.charAt(i)=='+' || s.charAt(i)=='-')
            a=1;
        while(a<s.length())
            if(s.charAt(a)=='0')
                a++;
            else 
                break;
        for(i=a;i<a+11 && i<s.length();i++)
        {
            if(s.charAt(i)>='0' && s.charAt(i)<='9')
                n=n*10+(int)(s.charAt(i)-'0');
            else
                break;
        }
        if(s.charAt(0)=='-')
            n=-n;
        if(n>2147483647)
            n=2147483647;
        if(n<-2147483648)
            n=-2147483648;
        return (int)n;
    }
}
class Solution
{
    public:
        int myAtoi(string s)
        {
            long long ans = 0;
            int neg = 1;
            int i = 0;
            while (i < s.length() and s[i] == ' ')
            {
                i++;
            }

            if (s[i] == '-' || s[i] == '+')
            {
                neg = s[i] == '-' ? -1 : 1;
                i++;
            }

            while (i < s.length() && (s[i] >= '0' && s[i] <= '9'))
            {

                ans = ans *10 + (s[i] - '0');
               	// ans *= 10;
                i++;
                if (ans * neg >= INT_MAX) return INT_MAX;
                if (ans * neg <= INT_MIN) return INT_MIN;
            }
            return ans * neg;
        }
};
const toNumber = (s) => {
    let res = 0;
    for (let i = 0; i < s.length; i++) {
        res = res * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
    }
    
    return res
}

var myAtoi = function(s) {
    s = s.trim();
    let r = s.match(/^(\d+|[+-]\d+)/);
    
    if (r) {
        let m = r[0], res;
        switch (m[0]) {
            case "-":
                res = toNumber(m.slice(1)) * -1;
                break;
            case '+':
                res = toNumber(m.slice(1));
                break;
            default:
                res = toNumber(m);
                break;
        }
        
        if (res < (1 << 31)) return (1 << 31);
        if (res > -(1 << 31) - 1) return -(1 << 31) - 1;
        return res;
    }
    
    return 0;
};
String to Integer (atoi)