代码随想录算法训练营第十天打卡

20.有效的括号

代码如下:

class Solution {
public:
    bool isValid(string s) { //说实话这题还是有难度的
        int size = s.size();
        stack<char> temp;
        if (size % 2) {
            return false;
        }
        for (int i = 0; i < size; i++) {
            if (s[i] == '(') {
                temp.push(')');
            } else if (s[i] == '[') {
                temp.push(']');
            } else if (s[i] == '{') {
                temp.push('}');
            } else if (temp.empty() || s[i] != temp.top()) {
                return false;
            } else {
                temp.pop();
            }
        }
        return temp.empty();
    }
};

完整代码如下:

#include <iostream>
#include <string>
#include <stack>

using namespace std;

class Solution {
public:
    bool isValid(string s) { //说实话这题还是有难度的
        int size = s.size();
        stack<char> temp;
        if (size % 2) {
            return false;
        }
        for (int i = 0; i < size; i++) {
            if (s[i] == '(') {
                temp.push(')');
            } else if (s[i] == '[') {
                temp.push(']');
            } else if (s[i] == '{') {
                temp.push('}');
            } else if (temp.empty() || s[i] != temp.top()) {
                return false;
            } else {
                temp.pop();
            }
        }
        return temp.empty();
    }
};

int main() {
    Solution sol;
    string s = "{[[{";
    bool ans = sol.isValid(s);
    cout << ans << endl;
    stack<char> s1;
    stack<char> s2;
    s1.push('a');
    s1.push('b');
    s2.push('a');
    s2.push('b');
    if (s1 == s2) {
        cout << true << endl;
    }
}

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

代码如下:

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

完整代码如下:

#include <iostream>
#include <string>
#include <stack>

using namespace std;

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

int main() {
    Solution sol;
    string s = "abbaca";
    string ans = sol.removeDuplicates(s);
    cout << ans << endl;
}

150.逆波兰表达式求值

代码如下:

class Solution {
public:
    int evalRPN(vector<string>& tokens) { //用栈来存储数字,如果遍历到了运算符,就把栈中的数字取出两个,然后进行运算,再将运算结果给栈。
        stack<long long> temp; //力扣要求用long long数据类型
        for (int i = 0; i < tokens.size(); i++) {
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { //注意这里是双引号,因为是字符串
                long long nums1 = temp.top();
                temp.pop();
                long long nums2 = temp.top();
                temp.pop();
                if (tokens[i] == "+") {
                    temp.push(nums2 + nums1);
                } else if (tokens[i] == "-") {
                    temp.push(nums2 - nums1);
                } else if (tokens[i] == "*") {
                    temp.push(nums2 * nums1);
                } else {
                    temp.push(nums2 / nums1);
                }
            } else {
                temp.push(stoll(tokens[i])); //stoll函数用来将字符串转成long long
            }
        }
        return temp.top();
    }
};

完整代码如下:

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

class Solution {
public:
    int evalRPN(vector<string>& tokens) { //用栈来存储数字,如果遍历到了运算符,就把栈中的数字取出两个,然后进行运算,再将运算结果给栈。
        stack<long long> temp; //力扣要求用long long数据类型
        for (int i = 0; i < tokens.size(); i++) {
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") { //注意这里是双引号,因为是字符串
                long long nums1 = temp.top();
                temp.pop();
                long long nums2 = temp.top();
                temp.pop();
                if (tokens[i] == "+") {
                    temp.push(nums2 + nums1);
                } else if (tokens[i] == "-") {
                    temp.push(nums2 - nums1);
                } else if (tokens[i] == "*") {
                    temp.push(nums2 * nums1);
                } else {
                    temp.push(nums2 / nums1);
                }
            } else {
                temp.push(stoll(tokens[i])); //stoll函数用来将字符串转成long long
            }
        }
        return temp.top();
    }
};

int main() {
    vector<string> tokens;
    tokens.push_back("4");
    tokens.push_back("13");
    tokens.push_back("5");
    tokens.push_back("/");
    tokens.push_back("+");
    Solution sol;
    int ans = sol.evalRPN(tokens);
    cout << ans << endl;
}

今毕。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值