Code Random Recording Algorithm Training Camp 15th Day 11 | 20. Valid parentheses, 1047. Remove all adjacent duplicates in a string, 150. Reverse Polish expression evaluation

20. Valid Parentheses 

After talking about stacks implementing queues and queues implementing stacks, the next step is the classic application of stacks. 

This question is an explanation of the three situations. 

Topic Link: Lituo

Code idea: First judge the length of the string. If the length of the string is odd, you can know that it obviously does not meet the meaning of the question. At this time, return false directly; then traverse the corresponding string and judge the three scenarios, which is how many In the case of parentheses, perform the corresponding push operation; (2 3 cases) Next, look for the stack. top() is different from the following, directly return false/if the stack is empty, also return false. (Case 1) The last step is to return whether the stack is empty.

class Solution {
public:
    bool isValid(string s) {

        //首先要进行判断奇数,如果要是奇数,直接返回false
        if(s.size()%2!=0)
        {
            return false;
        }

        //首先声明一个stack,用来存放对应的括号
        stack<char> panduan;

        for(int i = 0;i<s.size();i++)
        {
            if(s[i]=='(')
            {
                panduan.push(')');
            }
            else if(s[i]=='{')
            {
                panduan.push('}');
            }
            else if(s[i]=='[')
            {
                panduan.push(']');
            }

            else if(  panduan.empty()||s[i] != panduan.top() )//这个地方需要注意
            {
                return false;
            }

           else 
           {
               panduan.pop();
           }
        }
        return panduan.empty();
    }
};

1047. Remove all adjacent duplicates in a string --- Love Elimination Game

To know why the stack is suitable for this kind of operation similar to love elimination, because the stack helps us record what the previous element is when traversing the current element of the array.

Topic Link: Lituo

For example, as shown below: abbaca, after removing duplicates is c.

Idea: First, declare a stack to store strings, and ask whether the tail is the same as the current string. The same words perform a popup.

class Solution {
public:
    string removeDuplicates(string s) {

        //这道题是比较简单的

        //首先声明一个栈,用来存放相应的字符
        stack<char> cun;

        for(int i = 0;i<s.size();i++)
        {
            if(cun.empty()||cun.top()!=s[i])
            {
                cun.push(s[i]);
            }
            else
            {
                cun.pop();
            }
        }

        string result="";//存放返回值

        while(!cun.empty())//这个地方是不能够使用for进行判断的
        {
            result+=cun.top();
            cun.pop();
        }

        reverse(result.begin(),result.end());

        return result;


    }
};

150. Reverse Polish Expression Evaluation

This question is not difficult, but if you do it for the first time, it will be difficult to think of it, so watch the video first, understand the idea before doing the question

Link to this topic: Likou

class Solution {
public:
    int evalRPN(vector<string>& tokens) {

        //首先定义一个栈
        stack<long long> use;

        for(int i = 0;i<tokens.size();i++)
        {
            if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/")
            {
                long long k1 = use.top();
                use.pop();
                long long k2 = use.top();
                use.pop();
                if(tokens[i]=="-")
                {
                    use.push(k2-k1);
                }

                if(tokens[i]=="+")
                {
                    use.push(k2+k1);
                }

                if(tokens[i]=="*")
                {
                    use.push(k2*k1);
                }

                if(tokens[i]=="/")
                {
                    use.push(k2/k1);
                }
            }
            else
            {
                use.push(stoll(tokens[i]));//stoll 将string转换为long long类型
            }
        }
        int result = use.top();
        use.pop();

        return result;
    }
};

Guess you like

Origin blog.csdn.net/m0_47489229/article/details/131006246