【代码随想录训练营】Day11-栈与队列

代码随想录 Day11

今日任务

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

20. 有效的括号

链接:https://leetcode.cn/problems/valid-parentheses/
注意几种情况:
① 字符串长度为奇数
② 以右括号开头
③ 左括号比右括号多

class Solution {
    public boolean isValid(String s) {
        //(
        // if(s.length() == 1){
        //     return false;
        // }
        //长度为奇数一定不符合条件
        if(s.length() % 2 == 1){
            return false;
        }
        Stack<Character> pair = new Stack<Character>();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == '(' 
            || s.charAt(i) == '['
            || s.charAt(i) == '{'){
                pair.push(s.charAt(i));
            }
            else{
                // )[
                if(pair.isEmpty()){
                    return false;
                }
                else{
                    char cur = pair.pop();
                    if(s.charAt(i) == ')' && cur != '('){
                        return false;
                    }
                    else if(s.charAt(i) == ']' && cur != '['){
                        return false;
                    }
                    else if(s.charAt(i) == '}' && cur != '{'){
                        return false;
                    }
                }
            }
        }
        // ((
        if(!pair.isEmpty()){
            return false;
        }
        return true;
    }
}

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

链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
拓展;参考代码随想录Java写法
参考知识:https://blog.csdn.net/mingyuli/article/details/115830113
① ArrayDeque 是双端队列的数组实现,LinkedList 是双端队列的列表实现
② ArrayDeque 作为栈使用时比 Stack 效率高,作为队列时比 LinkedList 快
③ ArrayDeque 线程不安全,禁止 null

class Solution {
    public String removeDuplicates(String s) {
        if(s.length() == 1){
            return s;
        }
        //ArrayDeque 更快
        Stack<Character> stack = new Stack<Character>();
        stack.push(s.charAt(0));
        for(int i = 1; i < s.length(); i++){
            if(!stack.isEmpty() && s.charAt(i) == stack.peek()){
                stack.pop();
                continue;
            }
            else{
                stack.push(s.charAt(i));
            }
        }
        int len = stack.size();
        char[] result = new char[len];
        for(int i = len - 1; i >= 0; i--){
            result[i] = stack.pop();
        }
        return new String(result);
    }
}

字符串直接作为栈

class Solution {
    public String removeDuplicates(String s) {
        StringBuffer sb = new StringBuffer();
        int idx = -1;
        for(int i = 0; i < s.length(); i++){
            char cur = s.charAt(i);
            if(idx >= 0 && sb.charAt(idx) == s.charAt(i)){
                sb.deleteCharAt(idx);
                idx--;
            }
            else{
                sb.append(s.charAt(i));
                idx++;
            }
        }
        return sb.toString();
    }
}

双指针

class Solution {
    public String removeDuplicates(String s) {
        char[] sArray = s.toCharArray();
        int left = 0;
        int right = 1;
        while(right < s.length()){
            if(left >= 0 && sArray[left] == sArray[right]){
                left--;
            }
            else if(left >= 0 && sArray[left] != sArray[right]){
                sArray[++left] = sArray[right];
            }
            else if(left < 0){
                left = 0;
                sArray[left] = sArray[right];
            }
            right++;
        }
        return new String(sArray, 0, left + 1);
    }
}

150. 逆波兰表达式求值

链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/

class Solution {
    public int evalRPN(String[] tokens) {
        //用ArrayDeque会快很多
        //Stack<Integer> stack = new Stack<Integer>();
        ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
        for(int i = 0; i < tokens.length; i++){
            // if(tokens[i].charAt(0) != '+'
            // && tokens[i].charAt(0) != '-'
            // && tokens[i].charAt(0) != '*'
            // && tokens[i].charAt(0) != '/'){
            //     //字符串可能不是个位数
            //     stack.push(Integer.parseInt(tokens[i]));
            // }
            //正则表达式会慢很多
            // if(tokens[i].matches("-?[0-9]+")){
            //     stack.push(Integer.valueOf(tokens[i]));
            // }
            if(tokens[i].equals("+")){
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num1 + num2);
            }
            else if(tokens[i].equals("-")){
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 - num1);
            }
            else if(tokens[i].equals("*")){
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num1 * num2);
            }
            else if(tokens[i].equals("/")){
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 / num1); //注意这个顺序,先进去后出来的是分母
            }
            else{
                stack.push(Integer.valueOf(tokens[i]));
            }
        }
        return stack.pop();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值