Sword refers to Offer20-a string representing a numeric value C++

The last question of debt repayment

Title description

Insert picture description here
In a short description, there is no way to see what its rules are. It seems that everyone knows the rules. It is a very difficult and disgusting, not to say that it is a middle-level problem that is actually difficult.

Judge the solution one by one

There is a persuasive automaton in the solution. I always feel that there is no need to draw a whole state diagram for a problem (it is very lazy),
Insert picture description here
so I went shopping and commented, and finally saw a very simple and easy-to-understand one, and I was moved to cry. .
The big guy's thinking is very powerful, and he judges the right or wrong of each character's possible situation: return false directly when other situations occur.
For different situations and written in the code comments,
there is another point: C++ requires one more step compared to Java. It requires handwriting to remove the spaces at the beginning and end, and pay attention to the boundary judgment.

class Solution {
    
    
public:
    bool isNumber(string s) {
    
    
        if(s.size() == 0) return false;
        bool numFlag = false;
        bool dotFlag = false;
        bool eFlag = false;
        int index = 0;
        while(index < s.size() && s[index] == ' ') index ++;
        int begin = index;
        int end = s.size() - 1;
        while(end >= 0 && s[end] == ' ') end --;
        for(; index <= end; index++) {
    
    
            //numFlag判断规则,出现数字就标记
            if(isdigit(s[index])) {
    
    
                numFlag = true;
            }
            //.判定规则,没有出现过. 且必须出现在e的前面
            else if(s[index] == '.' && !dotFlag && !eFlag) {
    
    
                dotFlag = true;
            }
            //e判定规则,没有出现过e 且必须出现过数字
            else if((s[index] == 'e' || s[index] == 'E') && !eFlag && numFlag) {
    
    
                eFlag = true;
                numFlag = false;
            }
            // +/- 判定规则,只能出现在第一位或者在e后面
            else if((s[index] == '+' || s[index] == '-') && (index == begin || s[index - 1] == 'e' || s[index - 1] == 'E' )) {
    
    }
            else {
    
    
                return false;
            }
        }
        //如果字符合法,numflag结束之后必为true
        return numFlag;
    }
};

Insert picture description here
Here numFlag = falseis to prevent the judgment similar to '0e' as correct: that is, there must be a number after e to be correct.
Insert picture description here
Time complexity O(N)
Space complexity O(1)
Remember when the first question LeetCode was still a small Mengxin, even to find a character, you have to go through various writing methods of the same time complexity in an array hash table. In a blink of an eye, I have finished the offer of the sword finger. Soon, do it every day. Spring recruits are coming. . During the university, whether it was to draw water, earnestly earning points, or to study algorithms for work, I tried it all. I hope I can enter the big factory as I wish.
The gains are really a lot. At the beginning, the problems of algorithms and data structures were violent or violent. Now, I must feel that I am a fool. If you didn't listen to the seniors to study the algorithm problems, you would really be a rookie, let alone go to a big factory.
I feel that for the CS major, except for a few interview courses that are really meaningful, the other courses are not very rewarding. The grade point of the university can only prove one's own learning ability, but can't explain one's own strength.
It’s been a lot of relaxation after coming home for the past few days. I have to work out a question slowly a day, just like memorizing the last dozens of words in the sixth level, but anyway, I can hold on to one thing for three months. People, it's almost time to recruit in three months, and I continue to work hard to adjust the state.
There are still a lot of emotions. The summary is: a rookie reached the junior year and did an algorithm and discovered a new world, and regretted not doing it earlier.

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/113061809