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

20. 有效的括号

class Solution {
public:
    bool isValid(string s) {
        // 如果字符串是奇数,肯定不匹配
        if (s.size() % 2 != 0) return false;
        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('}');
            } else if (st.empty() || st.top() != s[i]) {
                return false;
            } else {
                st.pop();
            }
        }
        return st.empty();
    }
};

题目链接/文章讲解/视频讲解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html

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

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;

        for (int i = 0; i < s.size(); i++) {
            // 如果栈空或者栈顶不等于当前的元素,那么就进栈
            if (st.empty() || st.top() != s[i]) {
                st.push(s[i]);
            } else if (st.top() == s[i]) {
                // 如果相等就弹出
                st.pop();
            }
        }

        string result;
        // 依次取出栈中的元素
        while (!st.empty()) {
            result += st.top();
            st.pop();
        }
        // 还要再取反一下
        reverse(result.begin(), result.end());
        return result;
    }
};

题目链接/文章讲解/视频讲解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html

150. 逆波兰表达式求值

image.png

这里if判断是数字不好判断,因为操作符的情况好写,所以else是数字的情况。

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 a = st.top();
                st.pop();
                long long b = st.top();
                st.pop();
                if (tokens[i] == "+") st.push(b + a);
                else if (tokens[i] == "-") st.push(b - a);
                else if (tokens[i] == "*") st.push(b * a);
                else if (tokens[i] == "/") st.push(b / a);
            } else {
                st.push(stoll(tokens[i]));
            }

        }
        return st.top();
    }
};

题目链接/文章讲解/视频讲解:https://programmercarl.com/0150.%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B1%82%E5%80%BC.html

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 1. 给出一个字符串,找出字符串所有的单词。 可以通过正则表达式来实现,代码如下: ```javascript const str = "hello world, I'm a developer"; const words = str.match(/\b\w+\b/g); console.log(words); // ["hello", "world", "I'm", "a", "developer"] ``` 这里使用了 `\b\w+\b` 匹配单词的正则表达式,其 `\b` 表示单词边界,`\w+` 表示匹配一个或多个字母数字下划线字符。 2. 从键盘输入两个字符串,在第一个字符串删除第二个字符串的字符。 可以先将第二个字符串转换为字符数组,然后遍历该数组,在第一个字符串删除对应的字符。代码如下: ```javascript const str1 = "hello world"; const str2 = "o"; const chars = str2.split(""); for (let i = 0; i < chars.length; i++) { const char = chars[i]; str1 = str1.replace(new RegExp(char, "g"), ""); } console.log(str1); // "hell wrld" ``` 这里使用了 `split` 将第二个字符串转换为字符数组,然后遍历该数组,使用 `replace` 在第一个字符串删除对应的字符。 3. 使用正则表达式匹配日期格式,如 2022年11月30日。 可以使用如下正则表达式匹配日期格式: ```javascript const str = "2022年11月30日"; const dateRegex = /(\d{4})年(\d{1,2})月(\d{1,2})日/; const match = str.match(dateRegex); const year = match[1]; const month = match[2]; const day = match[3]; console.log(`${year}-${month}-${day}`); // "2022-11-30" ``` 这里使用了 `(\d{4})年(\d{1,2})月(\d{1,2})日` 匹配日期格式的正则表达式,其 `(\d{4})` 表示匹配 4 个数字的年份,`(\d{1,2})` 表示匹配 1 到 2 个数字的月份和日期。然后使用 `match` 方法匹配字符串,获取到匹配的年份、月份和日期,并使用模板字符串拼接成标准日期格式输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值