第11天-代码随想录刷题训练-第五章 栈和队列2-● 20. 有效的括号 ● 1047. 删除字符串中的所有相邻重复项 ● 150. 逆波兰表达式求值

1. 有效的括号

LeetCode链接

给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。

  • 遍历string获取到的元素是char类型的
  • 第2段代码是自己写的,第1段是看完卡哥的代码后修改的
class Solution {
public:


    // 1.如果字符串长度是奇数,那么一定是错的、
    // 2.如果在需要弹出栈的时候栈内为空,那么也是不对称的
    // 3.最后遍历完栈不为空,也是不对称的
    bool isValid(string s) {
        if(s.size() % 2 != 0){
            return false;
        }
        map<char, char> cm = {
            {')', '('},
            {']', '['},
            {'}', '{'}
        };
        stack<char> sc;
        for(auto c : s){
            if(cm.find(c) == cm.end()) sc.push(c);              // 如果是前半个括号则压入栈
            else if(sc.empty() || cm[c] != sc.top()) return false;  // 如果当前栈为空或者不等于栈顶的值
            else sc.pop();
        }
        
        return sc.empty();          // 如果为空返回true,不为空返回false
    }
};
class Solution {
public:
    // 1.如果字符串长度是奇数,那么一定是错的、
    // 2.如果在需要弹出栈的时候栈内为空,那么也是不对称的
    // 3.最后遍历完栈不为空,也是不对称的
    bool isValid(string s) {
        if(s.size() % 2 != 0){
            return false;
        }
        map<char, char> cm = {
            {')', '('},
            {']', '['},
            {'}', '{'}
        };
        stack<char> sc;
        for(auto c : s){
            if(cm.find(c) == cm.end()){ 
                sc.push(c);
            }else{          // 不是前半个括号
                if(sc.empty()){
                    return false;
                }
                char top = sc.top();
                sc.pop();
                if(top != cm[c]){
                    return false;
                }
            }
        }
        
        // 当sc不为空的时候,返回错误
        if(!sc.empty()){
            return false;
        }

        return true;
    }
};

2.删除字符串中的所有相邻重复项

LeetCode连接

给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
输入:“abbaca” 输出:“ca”

  • 自己写的使用string迭代器往begin() 添加char类型字符,不需要翻转, 但是空间使用多,因为每次添加多需要挪动数组,因此效率低下,不建议使用
class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> vc;
        for(auto c : s){
            if(!(vc.empty()) && vc.top()==c) vc.pop();      //如果栈不为空,并且栈内顶端元素和要压入栈的相同,则弹出
            else vc.push(c);
        }
        string result = "";
        while(!vc.empty()){
            result.insert(result.begin(), vc.top());
            vc.pop();
        }

        return result;
    }
};
class Solution {
public:
    string removeDuplicates(string S) {
        stack<char> st;
        for (char s : S) {
            if (st.empty() || s != st.top()) {
                st.push(s);
            } else {
                st.pop(); // s 与 st.top()相等的情况
            }
        }
        string result = "";
        while (!st.empty()) { // 将栈中元素放到result字符串汇总
            result += st.top();
            st.pop();
        }
        reverse (result.begin(), result.end()); // 此时字符串需要反转一下
        return result;

    }
};

3. 逆波兰表达式求值

LeetCode连接

给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。
输入:tokens = [“2”,“1”,“+”,“3”,“*”]
输出:9
解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9

  • string转int类型
  • long int stol(str, size_t * idx=0, base=10)
  • long long stoll(str, size_t * idx=0, base=10)
class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<long int> sli;
        for(auto c : tokens){
            if(c == "+" || c == "-" || c == "*" || c == "/"){
                long int num1 = sli.top();
                sli.pop();
                long int num2 = sli.top();
                sli.pop();
                if(c == "+") sli.push(num2 + num1);
                if(c == "-") sli.push(num2 - num1);
                if(c == "*") sli.push(num2 * num1);
                if(c == "/") sli.push(num2 / num1);
            }else{
                sli.push(stol(c));
            }
        }
        int result = sli.top();
        sli.pop();

        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值