LeetCode08, string to integer (atoi)

topic

Insert picture description here

prompt:

The blank characters in this question only include the space character ''.
Assuming that our environment can only store 32-bit signed integers, the value range is [−231, 231 − 1]. If the value exceeds this range, please 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-blank character is'- ', which is a negative sign.
We try our best to combine the minus sign with all subsequent numbers, and finally get -42.
Example 3:

Input: "4193 with words"
Output: 4193
Explanation: The conversion ends at the number '3' because the next character is not a number.
Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-blank character is'w', but it is not a number or a positive or negative sign.
Therefore, effective conversion cannot be performed.
Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" exceeds the range of a 32-bit signed integer.
Therefore INT_MIN (−2 31 ) is returned .
Passed 195,723 Submitted 938,532

1. Direct thinking-string regular matching

class Solution {
    
    
    public int myAtoi(String str) {
    
    
       str=str.stripLeading();//去除首部空格
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^[+|-]?\\d+");//\是转义字符,需要\\表示
        java.util.regex.Matcher matcher = pattern.matcher(str);
        //System.out.println(str.matches("^[+|-]?\\d+"));
        if(matcher.lookingAt()){
    
    //匹配成功,lookingAt()部分匹配,matches()是全部匹配
            str = str.substring(0,matcher.end());
            int res;
            int Limit = Integer.MAX_VALUE;
            try{
    
    
                if(str.charAt(0)=='-'){
    
    //是负数,可能越界值为 Integer.MIN_VALUE
                    Limit = Integer.MIN_VALUE;
                }//否则为正数
                res = Integer.parseInt(str);

            }catch (NumberFormatException e){
    
    
                res = Limit;
            }
            return res;
        }else
            return 0;



    }
}

Once accept, the efficiency is touching. It is estimated that the problem of quoting the class library.
Insert picture description here

2. If you have to make your own wheels

class Solution {
    
    
    public int myAtoi(String str) {
    
    
         //下面自己造轮子
        char[] chars = str.toCharArray();
        int start=-1;
        int flag=1;//判断是否为正数
        for (int i = 0; i < chars.length; i++) {
    
    //最多判断n次,就是全部是空格
            if(chars[i]==' '){
    
    //空格忽略
                continue;
            }
            if(chars[i]=='+'){
    
    //整数
                start = i;
                break;
            }
            if(chars[i]=='-'){
    
    //负数
                start = i;
                flag= -1;//只有这里可能是负数
                break;
            }
            if(!Character.isDigit(chars[i])){
    
    //不是数字或者+,-号开头则直接退出
                return 0;
            }
            if(Character.isDigit(chars[i])){
    
    //数字开头
                start = i;
                break;
            }
        }
        if(start<0)
            return 0;//全是空格
        if(chars[start]=='-'||chars[start]=='+') {
    
    
            start = start+1;
        }
        //也就是现在的start要么指向的是数字,要么指向的是符合

        int res=0;
        for(int i=start;i<chars.length;i++){
    
    
           if(Character.isDigit(chars[i])){
    
    //是数字则可以进行转化
               int pop = flag*(chars[i]-'0');
               //判断是否越界
               if (res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE / 10 && pop > 7))
                   return Integer.MAX_VALUE;
            
               if (res < Integer.MIN_VALUE/10 || (res == Integer.MIN_VALUE / 10 && pop < -8)) 
                   return Integer.MIN_VALUE;
               res = res*10+pop;//需要放后边,因为我们是先判断/10合要求了,那么它*10才会符合要求
           }else
               break;
        }
        return res;

    }
}

As expected: cowhide!

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44861675/article/details/108096397