This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

The title

Please implement a myAtoi(String s) function that converts a string to a 32-bit signed integer (similar to the AToi function in C/C++).

MyAtoi (string s) ¶

  • Read the string and discard useless leading Spaces
  • Check if the next character (assuming it is not at the end of the character) is a positive or negative sign and read the character (if any). Determine whether the final result is negative or positive. If neither is present, the result is assumed to be positive.
  • Reads the next character until the next non-numeric character is reached or the end of the input is reached. The rest of the string is ignored.
  • Convert the numbers read in in the previous step to integers (that is, “123” -> 123, “0032” -> 32). If no number is read, the integer is0. Change the symbol if necessary (starting with Step 2).
  • If the integer number exceeds the 32 bit signed integer range[- 231, 231-1), the integer needs to be truncated to keep it in this range. Specifically, less than- 231The integer of should be fixed to- 231, is more than231-1The integer of should be fixed to231-1
  • Returns an integer as the final result.

Note:

  • Whitespace characters in this topic include only the space character' '
  • Do not ignore any characters except the leading space or the rest of the string after a number.

 

Example 1:

Input: s = “42”

Output: 42

Explanation: The bold string is the character that has been read, and the caret is the character that is currently read. Step 1: “42” (no character is currently read in because there is no leading space) ^ Step 2: “42” (no character is currently read in because there is no ‘-‘ or ‘+’) ^ Step 3: “42” (read in “42”) ^ Parsed to give the integer 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″ (read the leading space, but ignore it) ^ Step 2:” **-**42″ (read the ‘-‘ character, so the result should be a negative number) ^ Step 3: “-42” (read “42”) ^ Parses to the integer -42. Since “-42” is in the range [-231, 231-1], the final result is -42.

Example 3:

Type: s = “4193 with words”

Output: 4193

Explanation:

Step 1: “4193 with words” (no character is currently read in, because there is no leading space) ^ Step 2: “4193 with words” (no character is currently read in, because there is no ‘-‘ or ‘+’) ^ Step 3: “4193 with words” (read “4193”; Since the next character is not a number, the reading stops.) ^ parses to the integer 4193. Since “4193” is in the range [-231, 231-1], the final result is 4193.

Example 4:

Type: s = “words and 987”

Output: 0

Explanation:

Step 1: “Words and 987″ (no characters are currently read in, because there is no leading space) ^ Step 2:” Words and 987″ (no characters are currently read in, because there is no ‘-‘ or ‘+’) ^ Step 3: “Words and 987” (the reading stops because the current character ‘w’ is not a number) ^ parses to the integer 0 because no number was read. Since 0 is in the range [-231, 231-1], the final result is 0.

Example 5:

Input: s = “-91283472332”

Output: – 2147483648

Explanation:

Step 1: “-91283472332” (no character is currently read in, because there is no leading space) ^ Step 2: “-91283472332” (the ‘-‘ character is read in, so the result should be a negative number) ^ Step 3: “-91283472332” (read “91283472332”) ^ Parse to the integer -91283472332. Since -91283472332 is less than the lower bound of the range [-231, 231-1], the final result is truncated to -231 = -2147483648.

 

Tip:

  • 0 <= s.length <= 200
  • sConsisting of letters (upper and lower case), numbers (0-9),' ','+',The '-''. 'composition

Their thinking

This question sets four states, corresponding to each step of the question

  • Find the handler corresponding to the state of the whitespace
  • Look for the state of the symbol
  • Look for the status of the numbers
  • End state -> The normal end state of a DFA is success state and failure state. In this case, we default success, because even if the DFA fails, we will return 0

code

function myAtoi(s: string) :number {
   let positive = true, ans = 0;
   s = s + '__'; // Add a sentinel to give the final-state handler a chance to execute

   let state = searchBlank;

   function dfa(){
       for(let char of s) {
           state = state && state(char); // DFA internal automatic torsion state}}function searchBlank(char) {
       if(char === ' ') return searchBlank; // If you find the space, keep looking for the space
       if(/ [+] /.test(char)) return blankEnd(char); // If it is found to be a symbol, it is handed over to the whitespace processing function (blackEnd)
       if(/\d/.test(char)) return getSymbol(char);
       return end; // If neither, then the character does not match the current state of the input set directly returns the finalstate
   }

   function blankEnd(char) { // After the space processing should find symbols and numbers
       if(/ \ + /.test(char)) return getSymbol; // If it is a sign, look for a number, and if it is a negative sign, change the value of positive
       if(/ / - /.test(char)) {
           positive = false;
           return getSymbol;
       }
       if(/\d/.test(char)) return getSymbol(char); // If it is found to be a number, it is directly handed over to the function getSymbol that handles the state of the number
       return end; // If the input set does not conform to the current state, return the final state directly
   }

   function getSymbol(char) {
       if(/ [0-9].test(char)) {
           ans = ans * 10 + Number(char);
           return getSymbol;
       }
       return end;// If the input set does not conform to the current state, return the final state directly
   }

   function end() { // End state, end operation
       if(ans === 0) return;
       if(! positive) { ans = -ans; } ans =Math.max(-Math.pow(2.31), ans);
       ans = Math.min(Math.pow(2.31) - 1, ans);
       return;
   }

   dfa();
   return ans;
};
Copy the code

The last

I dreamed of going all over the world with a sword

Take a look at the prosperity of the world

Young hearts are always a little frivolous

Just a man after all

No regrets I go my way

“Front brush” No.8