当前位置:网站首页>Nc100 converts strings to integers (ATOI)

Nc100 converts strings to integers (ATOI)

2022-07-01 12:34:00 I'm not Xiao Haiwa~~~~

 Insert picture description here
describe
Write a function StrToInt, Realize the function of converting string to integer . Out of commission atoi Or other similar library functions . The incoming string may consist of the following parts :
1. Several spaces
2.( Optional ) A symbolic character (’+’ or ‘-’)
3. Numbers , Letter , Symbol , A string expression consisting of spaces
4. Several spaces
 Insert picture description here
Example 1

 Input :
"82"
 Return value :
82

Example 2

 Input :
" -12 "
 Return value :
-12
 explain :
 Remove the space before and after , by -12  

Example 3

 Input :
"4396 clearlove"
 Return value :
4396
 explain :
6 The following character does not belong to a valid integer part , Remove , But return the valid part extracted earlier 

Example 4

 Input :
"clearlove 4396"
 Return value :
0

Example 5

 Input :
"-987654321111"
 Return value :
-2147483648

Code:

class Solution {
    
public:
    /** *  The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly  * * * @param s string character string  * @return int integer  */
    int StrToInt(string s) {
    
        // write code here
        int64_t num=0;
        int64_t min=-2147483648;
        int res;
        string s_="";
        for(int i=0;i<s.length();i++)
        {
    
            if(s[i]!=' ')
            {
    
                s_=s.substr(i);
                break;
            }
        }
        bool flag=true;
        if(s_.length()>2)
        {
    
            if(!(s_[1]>='0'&&s_[1]<='9')&&!(s_[0]>='0'&&s_[0]<='9'))
            {
    
                
                return 0;
            }
        }
        for(int i=0,j=0;i<s_.length();i++)
        {
    
            if(s_[i]=='-'&&i==0)
            {
    
                flag=false ;
                continue;
            }
            if(s_[i]=='+'&&i==0)
            {
    
                continue;
            }
            if(s_[i]>='0'&&s_[i]<='9')
            {
        
                num=num*10;
                num+=s_[i]-'0';
                int64_t temp=num;
                if(!flag)
                {
    
                    temp=0-num;      
                }
                if(temp<min)
                {
     
                    res=-2147483648;
                    cout<<res<<endl;
                    return res;
                }
                if(temp>(pow(2,31)-1))
                {
      
                    res=pow(2,31)-1;
                    cout<<res<<"====="<<endl;
                    return res;            
                }    
            }
            else
            {
    
                break;
            } 
        } 
        if(!flag)
        {
    
            num=0-num;
            
        }
        res=num;
        return res;
    }
};
原网站

版权声明
本文为[I'm not Xiao Haiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160026274647.html