String to integer (atoi)

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

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

  • Read in the string and discard useless leading spaces
  • Check whether 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 exists, the result is assumed to be positive.
  • Read the next character until you reach the next non numeric character or the end of the input. The rest of the string will be ignored.
  • Convert the 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 range of 32-bit signed integers [− 231, 231 − 1], the integer needs to be truncated to keep it within this range. Specifically, integers less than − 231 should be fixed to − 231, and integers greater than − 231 − 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 other characters except 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 there is no '-' or '+' here)
         ^
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 2:

Input: S = "- 42"
Output: - 42
 Explanation:
Step 1: "- 42" (read leading spaces but ignore them)
            ^
Step 2: "- 42" (read in '-' character, so the result should be negative)
             ^
Step 3: "- 42" (read in "42")
               ^
Parse to get the integer - 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"
         ^
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"
         ^
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 are no leading spaces)
         ^
Step 2: "- 91283472332" (read '-' 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.

 

Tips:

  • 0 <= s.length <= 200
  • s consists of English letters (uppercase and lowercase), numbers (0-9), ',' + ',' - 'and'. ' Composition
public class Solution8 {
	public int myAtoi(String s) {

		boolean start = false;
		s = s.trim();
		String temp = "";
		Character ch = '0';
		if (s.length() == 0) {
			return 0;
		}
		ch = s.charAt(0);
		if (ch.equals('+')) {
			start = false;
			for (int i = 1; i < s.length(); i++) {

				if (Character.isDigit(s.charAt(i))) {
					if (s.charAt(i) != '0') {
						start = true;
					}
					if (start == true) {
						temp = temp + s.substring(i, i + 1);
					}
				} else {
					break;
				}
			}
		}

		if (ch.equals('-')) {
			temp = "-";
			start = false;
			for (int i = 1; i < s.length(); i++) {

				if (Character.isDigit(s.charAt(i))) {
					if (s.charAt(i) != '0') {
						start = true;
					}
					if (start == true) {
						temp = temp + s.substring(i, i + 1);
					}
				} else {
					break;
				}
			}

		}
		if (Character.isDigit(s.charAt(0))) {
			start = false;
			for (int i = 0; i < s.length(); i++) {

				if (Character.isDigit(s.charAt(i))) {
					if (s.charAt(i) != '0') {
						start = true;
					}
					if (start == true) {
						temp = temp + s.substring(i, i + 1);
					}
				} else {
					break;
				}
			}

		}

		// System.out.println(temp);
		if (temp.length() == 0) {
			return 0;
		}
		if (temp.equals("-")) {
			return 0;
		}

		if (temp.length() > String.valueOf(Long.MAX_VALUE).length() - 1 && temp.charAt(0) == '-') {
			return Integer.MIN_VALUE;
		}
		if (temp.length() > String.valueOf(Long.MAX_VALUE).length() - 1 && temp.charAt(0) != '-') {
			return Integer.MAX_VALUE;
		}
		if (Long.parseLong(temp) > Integer.MAX_VALUE) {
			return Integer.MAX_VALUE;
		}
		if (Long.parseLong(temp) < Integer.MIN_VALUE) {
			return Integer.MIN_VALUE;
		}
		return Integer.parseInt(temp);
	}

	public static void main(String[] args) {

		Solution8 sol = new Solution8();

		String s = "  0000000000012345678";

		System.out.println(sol.myAtoi(s));
	}
}

 

Posted by fussy on Thu, 14 Apr 2022 15:39:39 +0930