String conversion integer (atoi) JAVA

Please implement an atoi function to convert a string to an integer.

First, the function discards useless beginning space characters as needed, until it finds the first non-space character. The following conversion rules are as follows:

If the first non-blank character is a positive or negative sign, combine this sign with as many consecutive numeric characters as possible to form a signed integer.
If the first non-blank character is a number, it is directly combined with the following consecutive number characters to form an integer.
The string may also have extra characters after the valid integer part, so these characters can be ignored and 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 blank characters, your function does not need to be converted, that is, it cannot be converted effectively.

In any case, if the function cannot perform a valid conversion, please return 0.

prompt:

本题中的空白字符只包括空格字符 ' ' 。
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231,  231 − 1]。如果数值超过这个范围,请返回  INT_MAX (231 − 1) 或 INT_MIN (−231) 。

Insert picture description here
Source: LeetCode
Link: https://leetcode-cn.com/problems/string-to-integer-atoi

Idea:
Given a string, we need to judge whether it can be converted into a signed integer, but there are the following requirements:

1.第一位为字母的直接return 0;
2.开始遍历数字后一旦遇到字母或者加减符号,直接退出循环;
3.如果数字大于Integer.MAX_VALUE,则直接返回Integer.MAX_VALUE;
4.如果数字大于Integer.MIN_VALUE,则直接返回Integer.MIN_VALUE;
5.为了更方便的的判断,我们要用trim()函数去掉左右两端的多余空格;

Code

class Solution {
    
    
    public int myAtoi(String str) {
    
    
        String res=str.trim();
        if(res.length()==0)
        return 0;  //处理为空的情况    
        //这里就是对于上面的第一种情况,第一位为字母的直接return 0;
        if((res.charAt(0)<'0'||res.charAt(0)>'9')&&(res.charAt(0)!='+'&&res.charAt(0)!='-'))  
        return 0;
         String ans="";//存储最后的答案
		//判断是否有符号,这里也会有+123这种情况
        if(res.charAt(0)=='-'||res.charAt(0)=='+'){
    
    
            ans+=res.charAt(0);
        for(int i=1;i<res.length();i++){
    
    
       		//是数字就添加
      		  if(res.charAt(i)>='0'&&res.charAt(i)<='9'){
    
    
                    ans+=res.charAt(i);
           			//我是直接取绝对值看是否超过Integer.MAX_VALUE再判断符号,
           			if(Math.abs(Long.parseLong(ans))>Integer.MAX_VALUE){
    
    
                    if(res.charAt(0)=='-')
                    return Integer.MIN_VALUE;
                    else return Integer.MAX_VALUE;
                    }
                }
                //不是符合直接退出
                else{
    
    
                    break;
                }
        }
        }
        //当第一位不是符号的时候
        else if((res.charAt(0)>='0'&&res.charAt(0)<='9')){
    
    
             
            for(int i=0;i<res.length();i++){
    
    
                if(res.charAt(i)>='0'&&res.charAt(i)<='9'){
    
    
                    ans+=res.charAt(i);
                    if(Long.parseLong(ans)>Integer.MAX_VALUE)
                    return Integer.MAX_VALUE;
                }
                else{
    
    
                    break;
                }
            }
        }
       //如果最后ans只有一位且为符号的时候,特殊情况
        if(ans.length()<=1&&(ans.contains("-")||ans.contains("+")))
       
        return 0;
        return Integer.parseInt(ans);
    }
}

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/108139207