2022-02-19Leetcode训练营_栈

天池训练营链接

天池leetcode训练营

最小栈

参考题解
建辅助栈,push的时候存储对应时间当前栈的最小值。

class MinStack {
    stack<int> s;
    stack<int> min_s;
public:
    MinStack() {
        min_s.push(INT_MAX);
        //push操作里面要比较
    }
    
    void push(int val) {
      s.push(val);
      min_s.push(min(val, min_s.top()));
    }
    
    void pop() {
        s.pop();
        min_s.pop();
    }
    
    int top() {
        return s.top();
    }
    
    int getMin() {
        return min_s.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

比较含退格的字符串

个人解答:
用例"a##c"
"#a#c"报错Process finished with exit code 139 (interrupted by signal 11: SIGSEGV),应该是越界

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        stack<char> s1,s2;
        for(int i=0;i<s.size();++i){
            if(s[i]!='#'){
                s1.push(s[i]);
            }
            else{
                s1.pop();
            }
        }
        for(int j=0;j<t.size();++j){
            if(t[j]!='#'){
                s2.push(t[j]);
            }
            else{
                s2.pop();
            }
        }
        //入栈
        if(s1.size()!=s2.size()){
            return false;
        }
        while(!s1.empty()){
            if(s1.top()!=s2.top()){
                return false;
            }
            s1.pop();
            s2.pop();
        }
        return true;
    }
};

改进:s1.pop()之前先判断是否空栈,是空就不处理,非空才pop

class Solution {
public:
    bool backspaceCompare(string s, string t) {
        stack<char> s1,s2;
        for(int i=0;i<s.size();++i){
            if(s[i]!='#'){
                s1.push(s[i]);
            }
            else{
                 if(!s1.empty()){
                    s1.pop();
                }
            }
        }
        for(int j=0;j<t.size();++j){
            if(t[j]!='#'){
                s2.push(t[j]);
            }
            else{
                if(!s2.empty()){
                    s2.pop();
                }
            }
        }
        //入栈
        if(s1.size()!=s2.size()){
            return false;
        }
        while(!s1.empty()){
            if(s1.top()!=s2.top()){
                return false;
            }
            s1.pop();
            s2.pop();
        }
        return true;
    }
};

基本计算器II

参考题解

class Solution {
public:
    int calculate(string s) {
        vector<int> st;
        char sign='+';
        int num_cur=0;
        for(int i=0; i<s.size(); ++i){
            //if(s[i]==' '){
            //    continue;
            //}
            //这样写会导致如果最后一堆空格,就没法将最后的结果算进去
            if(isdigit(s[i])){
                num_cur=num_cur*10+int(s[i]-'0');
                //这里不用int做类型转换会报错
            }
            if(!isdigit(s[i]) && s[i]!=' ' || i==s.size()-1){
                //这里是碰到下一个符号才会把前一个数字算进去,所以
                //最后一个数字没有下一个符号,要把结果算进去
                //空格跳过
                switch(sign){
                    case '+':
                        st.push_back(num_cur);
                        break;
                    case '-':
                        st.push_back(-num_cur);
                        break;
                    case '*':
                        st.back()*=num_cur;
                        break;
                        //前面的case要带break
                    default:
                        st.back()/=num_cur;
                        //default不带条件和break
                }
                num_cur=0;
                sign=s[i];
            }
        }
        return accumulate(st.begin(), st.end(), 0);
        //累加,起始是0            
    }
};

有效的括号

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        for(auto c:s){
            if(st.empty()){
                st.push(c);
                continue;
            }
            switch(c){
                case ')':
                    if(st.top() == '(') st.pop();
                    else st.push(c);
                    break;
                case ']':
                    if(st.top() == '[') st.pop();
                    else st.push(c);
                    break;
                case '}':
                    if(st.top() == '{') st.pop();
                    else st.push(c);
                    break;
                default:
                    st.push(c);
            }
        }
        return st.empty();
    }
};

逆波兰表达式求值

注意:if-else都规范写,
if
else if
else这样
写遍历用i=0 to n比c++的 auto c:string好用,因为后面可能会用到序号。

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        int n=tokens.size();
        stack<int> st;
        for(int i=0; i<n; ++i){
            if(tokens[i] == "+"){
                int num1=st.top();
                st.pop();
                int num2=st.top();
                st.pop();
                st.push(num1+num2);
            }
            else if(tokens[i] == "-"){
                int num1=st.top();
                st.pop();
                int num2=st.top();
                st.pop();
                st.push(num2-num1);
            }
            else if(tokens[i] == "*"){
                int num1=st.top();
                st.pop();
                int num2=st.top();
                st.pop();
                st.push(num1*num2);
            }
            else if(tokens[i] == "/"){
                int num1=st.top();
                st.pop();
                int num2=st.top();
                st.pop();
                st.push(num2/num1);
            }
            else{//数字就入栈
                st.push(stoi(tokens[i]));
            }
        }
        return st.top();
    }
};

知识

INT_MAX
C++ pop返回的void,要调栈顶得top才行
accumulate(st.begin(), st.end(), 0)
vector.back() vector.front()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值