# Strange upgrade - 8. String to integer (atoi)

Keywords: leetcode Letcode

# Title Description

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:

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 blank characters in this question only include the 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" (currently no characters are read in, because there is no '-' or '+' here)
^
^
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 2: "- 42" (read in the '-' character, 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.

Example 4:

Enter: s = "words and 987"
Output: 0
Explanation:
Step 1: "words and 987"
^
Step 2: "words and 987" (currently no characters are read in, because there is no '-' or '+' here)
^
Step 3: "words and 987" (reading stops because the current character 'w' is not a number)
^
The integer 0 was parsed because no number was read in.
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" (currently no characters are read in because there is no leading space)
^
Step 2: "- 91283472332" (read the '-' character, so the result should be negative)
^
Step 3: "- 91283472332" (read "91283472332")
^
The integer - 91283472332 is parsed.
Since - 91283472332 is less than the lower bound of the range [- 231, 231 - 1], the final result is truncated to - 231 = - 2147483648.

```package com.antg;

/**
* @Description:
* @Author Rabbit Antg
* @Date 2021/11/21
**/
public class Code_8 {
public static void main(String[] args) {
String a = "00000-42a1234";
System.out.println(myAtoi(a));
}

public static int myAtoi(String s) {
//Empty string detection
if(s==null||s.length()==0) return 0;

s = s.trim();

//Prevent empty strings after removing leading spaces
if(s==null||s.length()==0) return 0;

//Exclude letters beginning with letters
if((s.charAt(0)!='+'&&s.charAt(0)!='-')&&(s.charAt(0)<48||s.charAt(0)>57)){
return 0;
}
//Exclude the case where there is only a + - sign
if(s.length()==1){
if(s.charAt(0)=='+'||s.charAt(0)=='-') return 0;
}

//Traversal string
StringBuilder res = new StringBuilder();//container
boolean zeroAble = false;//Ability to accept 0
boolean flag = true;//Ability to accept signs
for(int i = 0;i<s.length();i++){
char c = s.charAt(i);
//Symbol
if(c=='+'||c=='-'){
if(res.length()==0&&flag){
res.append(c);
continue;
}
}

if(zeroAble){
if(c>=48&&c<=57){
res.append(c);
}else{
break;
}
}else{
if(c==48){
flag = false;
continue;
}
if(c>48&&c<=57){
res.append(c);
//Once you have a number, you can store 0
zeroAble = true;
}else {
break;
}
}
}

int result = 0;
try {
if(res.length()==0)return 0;
if(res.length()==1&&(res.charAt(0)=='+'||res.charAt(0)=='-')){
return 0;
}
result = Integer.parseInt(res.toString());
} catch (NumberFormatException e) {
if(res.toString().startsWith("-")){
return Integer.MIN_VALUE;
}else{
return Integer.MAX_VALUE;
}
}
return result;
}
}

```

Posted by AL123 on Fri, 26 Nov 2021 18:20:30 -0800