This is the seventh day of my participation in the More text Challenge. For details, see more text Challenge

The title

When the first non-null character we find is a positive or negative sign, we combine that symbol with as many consecutive numbers as possible to form the positive or negative sign of the integer. If the first non-null character is a number, it is directly combined with subsequent numeric characters to form an integer.

The string may also have extra characters beyond the valid integer part, and these characters can be ignored; they should not affect the function.

Note: If the first non-space character in the string is not a valid integer character, the string is empty, or the string contains only whitespace characters, then your function does not need to convert.

In any case, return 0 if the function does not perform a valid conversion.

Description:

Assuming that our environment stores only 32 bit sized signed integers, the values range from [−231, 231 − 1]. If the value is outside this range, return INT_MAX (231 − 1) or INT_MIN (−231).

Example 1:

Input: “42” Output: 42

Example 2:

Input: “-42” Output: -42 Explanation: The first non-whitespace character is ‘-‘, which is a minus sign. We combined the negative sign with all the consecutive numbers that we could, and we ended up with -42.

Example 3:

Input: “4193 with words” Output: 4193 Explanation: The conversion ends with the number ‘3’ because its next character is not a number.

Example 4:

Input: “Words and 987” Output: 0 Explanation: The first non-null character is ‘w’, but it is not a number or a positive or negative sign. Therefore, a valid transformation cannot be performed.

Example 5:

Input: “-91283472332” Output: -2147483648 Description: The number “-91283472332” exceeds the 32-bit signed integer range. So return INT_MIN (−231)

Source: LeetCode

Train of thought

1. Use trim() to remove the Spaces before and after, filter out illegal cases (the first character can only be a number or + or -), and use a variable f1 to record whether the number is negative

2. Iterate through the remaining characters and get the number res in this form

 res = (res * 10 + (chars[i] - '0'));
Copy the code

3. The key is to judge the two cases which are out of the range of 32 bit signed integer according to the case of F1 respectively

                if (f1) {// If it's negative
                    if (-1 * res < Integer.MIN_VALUE / 10 || Integer.MIN_VALUE + res * 10 + (chars[i] - '0') > =0) {
                        returnInteger.MIN_VALUE; }}else {// Not a negative number
                    if (res > Integer.MAX_VALUE / 10 || res * 10 >= Integer.MAX_VALUE - (chars[i] - '0')) {
                        returnInteger.MAX_VALUE; }}Copy the code

4. When returning at last, it is necessary to determine whether to multiply by -1 according to F1;

code


    class Solution {
        public int strToInt(String str) {

            str = str.trim();// Remove the Spaces before and after
            if (str == null || str.length() == 0) return 0;
            char[] chars = str.toCharArray();
            if (chars[0] != '+' && chars[0] != The '-' && (chars[0] < '0' || chars[0] > '9')) return 0;
            boolean f1 = false;// Indicates whether it is a negative number
            int res = 0;
            if (chars[0] = =The '-') {
                f1 = true;
            } else if (chars[0] = ='+') {}else {/ / is a number
                res = (int) (chars[0] - '0');
            }
            for (int i = 1; i < chars.length; i++) {
                if (chars[i] < '0' || chars[i] > '9') break;
                if (f1) {// If it's negative
                    if (-1 * res < Integer.MIN_VALUE / 10 || Integer.MIN_VALUE + res * 10 + (chars[i] - '0') > =0) {
                        returnInteger.MIN_VALUE; }}else {// Not a negative number
                    if (res > Integer.MAX_VALUE / 10 || res * 10 >= Integer.MAX_VALUE - (chars[i] - '0')) {
                        return Integer.MAX_VALUE;
                    }
                }
                res = (res * 10 + (chars[i] - '0'));
            }
            return f1 ? -1* res : res; }}Copy the code