首页 > 编程语言 >代码随想录算法训练营第十一天| 20. 有效的括号 1047. 删除字符串中的所有相邻重复项 150. 逆波兰表达式求值

代码随想录算法训练营第十一天| 20. 有效的括号 1047. 删除字符串中的所有相邻重复项 150. 逆波兰表达式求值

时间:2024-03-16 11:24:10浏览次数:22  
标签:第十一天 String int 随想录 pop parseInt 求值 Integer stack

20. 有效的括号
https://leetcode.cn/problems/valid-parentheses/description/

public boolean isValid(String s) {
        if (s == null) return true;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '['){
                stack.push(c);
            }else {
                switch (c){
                    case ')' : if (!stack.isEmpty() && stack.peek() == '('){
                        stack.pop();
                    }else {
                        return false;
                    }
                    break;
                    case '}' : if (!stack.isEmpty() && stack.peek() == '{'){
                        stack.pop();
                    }else {
                        return false;
                    }
                    break;
                    case ']' : if (!stack.isEmpty() && stack.peek() == '['){
                        stack.pop();
                    }else {
                        return false;
                    }
                    break;
                }
            }
        }
        if (stack.size() == 0){
            return true;
        }else {
            return false;
        }
    }

总结:用栈做括号匹配,经典问题。
1047. 删除字符串中的所有相邻重复项
https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/

public String removeDuplicates(String s) {
        StringBuffer stack = new StringBuffer();
        int top = -1;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (top == -1){
                stack.append(c);
                top++;
            }else if (stack.charAt(top) == c){
                stack.deleteCharAt(top);
                top--;
            }else {
                stack.append(c);
                top++;
            }
        }
        return stack.toString();
    }

总结:切记,栈是逻辑上的,真正的代码实现你可以用Stack,StringBuffer,甚至是队列去实现
150. 逆波兰表达式求值
https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/

public int evalRPN(String[] tokens) {
        Deque<String> stack = new LinkedList<>();
        for (String token : tokens) {
            if (!stack.isEmpty()){
                switch (token){
                    case "+" : {
                        int num2 = Integer.parseInt(stack.pop());
                        int num1 = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1 + num2));
                        break;
                    }
                    case "-" : {
                        int num2 = Integer.parseInt(stack.pop());
                        int num1 = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1 - num2));
                        break;
                    }
                    case "*" : {
                        int num2 = Integer.parseInt(stack.pop());
                        int num1 = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1 * num2));
                        break;
                    }
                    case "/" : {
                        int num2 = Integer.parseInt(stack.pop());
                        int num1 = Integer.parseInt(stack.pop());
                        stack.push(String.valueOf(num1 / num2));
                        break;
                    }
                    default : stack.push(token);
                }
            }else {
                stack.push(token);
            }
        }
        return Integer.parseInt(stack.pop());
    }

总结: 栈的物理实现用deque比stack快得多 实例化是LinkedList或ArrayDeque

标签:第十一天,String,int,随想录,pop,parseInt,求值,Integer,stack
From: https://www.cnblogs.com/jeasonGo/p/18076843

相关文章