第十一天 | 20.有效的括号 1407.删除字符串中所有的相邻重复项 150.逆波兰表达式求值

今天的题思路都是清晰的,主要看细节

题目:20.有效的括号

第一次写的错误代码:

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        for(int i = 0; i < s.size(); i++){
            if(s[i] == '(' ){
                st.push(')');
            }
            else if(s[i] == '['){
                st.push(']');
            }
            else if(s[i] == '{'){
                st.push('}');
            }
            // if(s[i] == ')'){
            //     if(st.top() != '('){
            //         return false;
            //     }
            //     st.pop();
            // }
            // if(s[i] == ']'){
            //     if(st.top() != '['){
            //         return false;
            //     }
            //     st.pop();
            // }
            // if(s[i] == '}'){
            //     if((st.top() != '{')){
            //         return false;
            //     }
            //     st.pop();
            // }
            else if(st.empty() || st.top() != s[i]) return false;
            else st.pop();
        }
        return st.empty();                //每有考虑左侧括号有多的现象,右侧的每一个括号都可以一一配对,但左侧会多出来几个没有被配对的括号
    }
};

dubug了很久才改对,因为始终没有把if和else if的差别当回事,如果用if,在逻辑上当s[i] == ( [ { 时都会进行     else if(st.empty() || st.top() != s[i]) return false; 所以一定返回的是false.

题目: 1407.删除字符串中所有的相邻重复项 

尝试解答代码如下:出现访问越界问题

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for(int i = s.size() - 1; i >=  0; i++){
            if(!st.empty() && st.top() == s[i]){
                st.pop();
            }
            st.push(s[i]);
        }
        for(int i = 0; i < s.size(); i++){
            if(!st.empty()){
                s[i] = st.top();
                st.pop();
            }else{
                s[i] = '/0';
            }
        }
        return s;
    }
};

for做倒叙循环,故应该是i--;

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for(int i = s.size() - 1; i >=  0; i--){
            if(!st.empty() && st.top() == s[i]){
                st.pop();
            }else{
                st.push(s[i]);
            }
        }
        // for(int i = 0; i < s.size(); i++){
        //     if(!st.empty()){
        //         s[i] = st.top();
        //         st.pop();
        //     }else{
        //         s[i] = '/0';
        //     }
        // }
        // return s;             //按照注释掉的写法会多输出一些0
        string result = "";
        while(!st.empty()){
            result += st.top();
            st.pop();
        }                   
        return result;
    }
};

题目:150.逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<long long> st;
        for(int i = 0; i < tokens.size(); i++){
            if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
                long long c1 = st.top();
                st.pop();
                long long c2 = st.top();
                st.pop();
                if(tokens[i] == "+"){
                    st.push(c1 + c2);       //运算数的顺序有误,对加法和乘法没有影响,对减法和除法有影响。
                }
                if(tokens[i] == "-"){
                    st.push(c1 - c2);       //先弹出来的作被减数,所以应该是c2 - c1
                }
                if(tokens[i] == "*"){
                    st.push(c1 * c2);
                }
                if(tokens[i] == "/"){
                    st.push(c1 / c2);
                }
            }else{
                st.push(stoll(tokens[i]));     //此处需要用stoll函数,应该测试集包含longlong类型的数
            }
        }
        
        return st.top();
    }
};

改正后的代码

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<long long> st;
        for(int i = 0; i < tokens.size(); i++){
            if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
                long long c1 = st.top();
                st.pop();
                long long c2 = st.top();
                st.pop();
                if(tokens[i] == "+"){
                    st.push(c1 + c2);
                }
                if(tokens[i] == "-"){
                    st.push(c2 - c1);
                }
                if(tokens[i] == "*"){
                    st.push(c1 * c2);
                }
                if(tokens[i] == "/"){
                    st.push(c2 / c1);
                }
            }else{
                st.push(stoll(tokens[i]));
            }
        }
        
        return st.top();
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值