【算法日记】力扣20.有效的括号 1047.删除字符串中所有相邻重复项 150.逆波兰表达式求值

力扣20.有效的括号 1047.删除字符串中所有相邻重复项 150.逆波兰表达式求值

力扣20.有效的括号

题目

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。

示例 1:

**输入:**s = “()”

**输出:**true

示例 2:

**输入:**s = “()[]{}”

**输出:**true

示例 3:

**输入:**s = “(]”

**输出:**false

示例 4:

**输入:**s = “([])”

**输出:**true

提示:

  • 1 <= s.length <= 104
  • s 仅由括号 '()[]{}' 组成

思路

使用栈来做括号匹配

源码

class Solution {
public:
    bool isLeft(char c) { return c == '(' || c == '[' || c == '{'; }
    bool isMatch(char l, char r) {
        if (l == '{' && r == '}') return true;
        if (l == '(' && r == ')') return true;
        if (l == '[' && r == ']') return true;
        return false;
    }

    bool isValid(string s) {
        stack<char> charater;
        for (int i = 0; i < s.size(); ++i) {
            if (isLeft(s[i])) {
                charater.push(s[i]);
            } else {
                if (charater.empty()) return false;
                char top = charater.top();
                if (isMatch(top, s[i])) {
                    charater.pop();
                } else return false;
            }
        }
        if (charater.empty()) return true;
        else return false;
    }
};

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

题目

给出由小写字母组成的字符串 s重复项删除操作会选择两个相邻且相同的字母,并删除它们。

s 上反复执行重复项删除操作,直到无法继续删除。

在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

示例:

输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。

提示:

  1. 1 <= s.length <= 105
  2. s 仅由小写英文字母组成。

思路

使用栈来消消乐

源码

#include <bits/stdc++.h>
using namespace std;

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

int main() {
    Solution s;
    cout << s.removeDuplicates("abbaca") << endl;
    return 0;
}

150.逆波兰表达式求值

题目

给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。

请你计算该表达式。返回一个表示表达式值的整数。

注意:

  • 有效的算符为 '+''-''*''/'
  • 每个操作数(运算对象)都可以是一个整数或者另一个表达式。
  • 两个整数之间的除法总是 向零截断
  • 表达式中不含除零运算。
  • 输入是一个根据逆波兰表示法表示的算术表达式。
  • 答案及所有中间计算结果可以用 32 位 整数表示。

示例 1:

输入:tokens = ["2","1","+","3","*"]
输出:9
解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9

示例 2:

输入:tokens = ["4","13","5","/","+"]
输出:6
解释:该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6

示例 3:

输入:tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
输出:22
解释:该算式转化为常见的中缀算术表达式为:
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

提示:

  • 1 <= tokens.length <= 104
  • tokens[i] 是一个算符("+""-""*""/"),或是在范围 [-200, 200] 内的一个整数

思路

也是使用栈

源码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;

class Solution {
public:
    ll string2ll(string num) {
        bool isUnder0 = false;
        if (num[0] == '-') {
            num[0] = '0';
            isUnder0 = true;
        }
        ll res = 0;
        for (int i = 0; i < num.size(); ++i) {
            res = res * 10 + (num[i] - '0');
        }
        if (isUnder0) res = -res;
        return res;
    }
    string ll2string(ll num) {
        bool isUnder0 = num < 0;
        string res;
        if (isUnder0) {
            res.push_back('-');
            num = -num;
        }
        stack<int> num_st;
        while (num) {
            num_st.push(num % 10);
            num /= 10;
        }
        while (!num_st.empty()) {
            res.push_back(num_st.top() + '0');
            num_st.pop();
        }
        return res;
    }
    string stringCaculate(const string a, const string b, const string ops) {
        string res;
        ll A = string2ll(a);
        ll B = string2ll(b);
        ll RES;
        if (ops == "+") {
            RES = A + B;
        } else if (ops == "-") {
            RES = A - B;
        } else if (ops == "*") {
            RES = A * B;
        } else if (ops == "/") {
            RES = A / B;
        }
        res += ll2string(RES);
        return res;
    }
    bool isOperator(const string str) {
        return str == "+" || str == "-" || str == "*" || str == "/";
    }

    ll evalRPN(vector<string>& tokens) {
        stack<string> st;
        for (int i = 0; i < tokens.size(); ++i) {
            if (st.empty()) {
                st.push(tokens[i]);
            } else {
                if (isOperator(tokens[i])) {
                    string num_a = st.top();
                    st.pop();
                    string num_b = st.top();
                    st.pop();
                    st.push(stringCaculate(num_b, num_a,tokens[i]));
                } else st.push(tokens[i]);
            }
        }
        return string2ll(st.top());
    }
};

int main() {
    Solution s;
    vector<string> tokens1 = {"-128","-128","*","-128","*","-128","*","-1","*","8","*"};
    cout << s.evalRPN(tokens1) << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值