String to integer (atoi)

Keywords: Java Algorithm leetcode

Title:

Please realize one   myAtoi(string s)   Function to convert a string into a 32-bit signed integer (similar to the atoi function in C/C + +).

function   The algorithm of myAtoi(string s) is as follows:

Read in the string and discard useless leading spaces
Check whether the next character (assuming it has not reached the end of the character) is a positive or negative sign, and read the character (if any). Determines whether the final result is negative or positive. If neither exists, 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 will be ignored.
Convert these numbers read in the previous steps into integers (i.e., "123" - > 123, "0032" - > 32). If no number is read in, the integer is 0. Change the symbol if necessary (starting from step 2).
If the number of integers exceeds the 32-bit signed integer range [− 231,    two hundred and thirty-one   − 1], you need to truncate this integer to keep it within this range. Specifically, integers less than − 231 should be fixed to − 231 and greater than 231   The integer of − 1 should be fixed to 231   − 1 .
Returns an integer as the final result.
be careful:

The white space character in this question only includes the white space character ''.
Do not ignore any characters other than the leading space or the rest of the string after the number.
 

Example   1:

Input: s = "42"
Output: 42
Explanation: the bold string is the character that has been read in, and the caret is the character that is currently read.
Step 1: "42" (no characters are currently read in because there are no leading spaces)
         ^
Step 2: "42" (no characters are currently read in because '-' or '+' does not exist here)
         ^
Step 3: "42" (read "42")
           ^
The integer 42 is parsed.
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 leading spaces but ignore them)
            ^
Step 2:“   - 42 "(read '-' characters, so the result should be negative)
             ^
Step 3:“   - 42 "(read in" 42 ")
               ^
The integer - 42 is parsed.
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"
         ^
Step 2: "4193 with words"
         ^
Step 3: "4193 with words"
             ^
The integer 4193 is parsed.
Since "4193" is in the range [- 231, 231 - 1], the final result is 4193.

code:

/**
 * Created by 86130 on 2021/11/29.
 */
public class Atoi {
    public static int myAtoi(String s) {
        char[] charArray = s.toCharArray();
        int index = -1;
        //The first step is to find the first character, which is no longer a space
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] != ' ') {
                break;
            }
            index = i;
        }

        //If all are spaces, 0 is returned directly
        if (index == charArray.length - 1) {
            return 0;
        }
        //Navigate to the first character that is not a space
        index++;
        //true is positive and false is negative
        boolean flag = true;
        //Plus or minus, plus one
        if (charArray[index] == '-') {
            flag = false;
            index++;
        } else if (charArray[index] == '+') {
            index++;
        }
        //Define a long integer
        long result = 0;
        for (int i = index; i < charArray.length; i++) {
            char ch = charArray[i];
            if (ch >= '0' && ch <= '9') {
                result = result * 10 + ch - '0';
                //Exit directly if it is greater than the maximum value of Inter
                if (result > Integer.MAX_VALUE) {
                    break;
                }
            } else {
                //Non digital direct exit
                break;
            }
        }

        //Return value as required
        result = flag ? result : -result;
        if (result > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        if (result < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }
        return (int) result;
    }

    public static void main(String[] args) {
        System.out.print(myAtoi("-9223372036854775808"));
    }

}

Posted by bryson on Mon, 29 Nov 2021 13:28:35 -0800