Sword finger Offer 67 Convert string to integer

Posted by supermars on Thu, 03 Feb 2022 20:23:54 +0100

Title Description:

Write a function StrToInt to realize the function of converting string into integer. atoi or other similar library functions cannot be used.

First, the function discards useless opening space characters as needed until the first non space character is found.

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

In addition to the valid integer part of the string, there may also be redundant characters. These characters can be ignored and should not affect the function.

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

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

explain:

Assuming that our environment can only store 32-bit signed integers, the range of values 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 to combine the minus sign with all the subsequent consecutive numbers to get - 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:

Enter: "words and 987"
Output: 0
Explanation: the first non empty character is' w ', but it is not a number or a positive or negative sign.
Therefore, a valid conversion cannot be performed.
Example 5:

Input: "- 91283472332"
Output: - 2147483648
Explanation: the number "- 91283472332" exceeds the range of 32-bit signed integers.  
Therefore, int is returned_ MIN (−231) .

Source: LeetCode
Link: https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Analysis: I'm really tired of this string verification and matching problem. It's so troublesome. There are boundaries everywhere. To be honest, I've rewritten this problem according to each test case that causes errors. I'm really tired.... Especially the last few test cases, which are changed one by one. Each test case can find the defects in my program

Not much to say. Let's talk about the excellent solution of code, annotation and problem solution tomorrow. It's really too late

class Solution {
public:
    int strToInt(string str) {
        bool firstblank=true;
        bool ifhasfuhao=false;
        bool firstzero=true;
        int firstzeroindex=2147483647;
        int firstfuhaoindex=2147483647;
        int firstblankindex=2147483647;
        bool ifhasshuzi=false;
        char fuhao=' ';
        string tempres;
        for(int i=0;i<str.size();i++)
        {
            if(str[i]==' ')
            {
                if(ifhasshuzi) break;
                if(firstblank)
                {
                    continue;
                }
                else
                    break;

            }
            if((str[i]=='+'||str[i]=='-'))
            {
                if(ifhasshuzi) break;
                if(!ifhasfuhao)
                {
                    if(firstzeroindex<firstfuhaoindex)
                    return 0;
                    fuhao=str[i];
                    ifhasfuhao=true;
                    firstblank=false;
                    firstfuhaoindex=i;
                    continue;
                }
                else
                    break;

            }
            if(str[i]>='0'&&str[i]<='9')
            {
                ifhasshuzi=true;
                if(str[i]=='0'&&firstzero)
                {
                    firstzeroindex=i;
                    continue;

                }
                
                tempres+=str[i];
                firstblank=false;
                firstzero=false;
                continue;
            }
            else
                break;
        }
        // cout<<tempres<<endl;
        if(tempres=="") return 0;
        return converse(tempres,fuhao);
    }
    int converse(string str,char fuhao)
    {
        int start=0;
        if(fuhao==' ')
            fuhao='+';
        int len=str.size();
        if(len>10&&fuhao=='-') 
            return -2147483648;
        else if(len>10)
            return 2147483647;
        unsigned long long int res=0;
        unsigned long long int base=1;
        for(int i=len-1;i>=0;i--)
        {
            char c=str[i];
            int temp=c-'0';
            res=res+temp*base;
            base=base*10;

        }
        if(res>2147483648&&fuhao=='-')
            return -2147483648;
        if(res>2147483647&&fuhao=='+')
            return 2147483647;
        if(fuhao=='-')
            return -1*res;
        else
            return res;
        return 0;


        

    }
};

 

Topics: C++ leetcode Back-end