当前位置:网站首页>[Niuke] convert string to integer

[Niuke] convert string to integer

2022-06-24 08:59:00 Uaena_ An

Convert a string to an integer

 Insert picture description here

🧸 Reading questions

String reshaping , Encounter is not +- Other characters of the return directly 0;
And here it is +- Is a positive or negative number , Positive numbers should be omitted + , Negative numbers are displayed -.

🧸 Code

My idea is , Whatever , Go straight to , Return... When encountering other characters 0
When the result is required to be Positive numbers , Output directly .
If it is negative , Just subtract from the positive number 2 times Oneself

class Solution {
    
public:
    bool isNumber(char ch)
    {
    
        if(ch >= '0' && ch<= '9')
        {
    
            return true;
        }
        else if( ch == '+' || ch =='-')
        {
    
            return true;
        }
        return false;
    }
    int StrToInt(string str) {
    
        //'1' - '0' = 1
        int end = str.size()-1;
        int ret = 1;// Calculate the number of digits 
        int count = 0;// Count 
        while(0 <= end)
        {
    
            while(0 <= end && !isNumber(str[end]))
            {
    
                return 0;
            }
            if(str[end] != '+'&& str[end]!='-')
            {
    
                count += (str[end] - '0')*ret;
                ret*=10;
            }
            --end;
        }
        if(str[0] == '-')
        {
    
            count -= 2*count; 
        }
        return count;
    }
};

🧸 Decoding code

class Solution {
    
public:
	 It's a judgment   A function of numbers    It's the number. /+/- return true, Otherwise return to false
    bool isNumber(char ch)
    {
    
        if(ch >= '0' && ch<= '9')
        {
    
            return true;
        }
        else if( ch == '+' || ch =='-')
        {
    
            return true;
        }
        return false;
    }
    int StrToInt(string str) {
    
        //'1' - '0' = 1
     Starting from the tail 
        int end = str.size()-1;
        int ret = 1;// Calculate the number of digits 
        int count = 0;// Count 
	end Traverse from back to front ,end Less than 0 when   Then stop , be equal to 0  The description has another character , Go in, too 
        while(0 <= end)
        {
    
   	 Determine if there are other characters , Other characters are returned directly 0
            while(0 <= end && !isNumber(str[end]))
            {
    
                return 0;
            }
     There are no other characters here   Or numbers , Or +-
     Judgment is not +-  Just go in 
            if(str[end] != '+'&& str[end]!='-')
            {
    
     Come here ,ret The initial value must be 1
     the reason being that 0 Words   All the time 0
                count += (str[end] - '0')*ret;
     You are the last , So the last one *1 Or you guys 
     The second time *10  So here ret*=10
                ret*=10;
            }
     It's a joke ,end Go forward 
            --end;
        }
     Here is a negative number , If the first one is  -  Then subtract 2 Times your own 
        if(str[0] == '-')
        {
    
            count -= 2*count; 
        }
        return count;
    }
};

I don't think much about the code of other big guys , Tested it , Very large numbers are an effect , I just can't figure it out . The algorithm is O(N).

come on. I wish you get what you want offer!

原网站

版权声明
本文为[Uaena_ An]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240640397540.html