算法训练第11天 | 栈与队列part02

LeetCode20、有效的括号

代码
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (c == '('){
                stack.push(')');
            }else if (c == '['){
                stack.push(']');
            }else if (c == '{'){
                stack.push('}');
            }else {
                if ((stack.isEmpty()) || (c != stack.peek())){
                    return false;
                }
                stack.pop();
            }
        }
        if (stack.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
}
要点
  1. 这里要先判断是否为空,再取stack的栈顶值,如果顺序调换的话,如果stack已经空了,但先取栈顶值的话,就会报错了
  2. for循环结束之后,不能马上返回TRUE,因为有可能for循环结束了,栈还不为空,(对应左边多的情况),此时要返回FALSE
  3. 总结下来就是三种情况:左边多符号,右边多符号,中间的符号不配对
  4. ({{}[]})

LeetCode1047、删除字符串中的所有相邻重复项

代码
class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if ((stack.isEmpty()) || (c != stack.peek())){
                stack.push(c);
            }else {
                stack.pop();
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        sb = sb.reverse();
        return sb.toString();
    }
}
要点

这道题绕了好久,把自己绕晕进去了,原来采用的方法是如果空,则push;不空,则进循环
在这里插入图片描述
但是总是在八个a的测试用例那里卡住,输出结果怎么改也是要输出1个a,没弄清什么情况。。。

后来换了代码随想录里的思路,push的情况:stack为空或者字母不匹配;pop的情况:else。代码简单还解决了问题。

LeetCode150、逆波兰表达式求值

代码

我的代码

public class Stack150 {
    public int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++){
            int num1, num2, result;
            num2 = 0;
            if ((!stack.isEmpty()) && (tokens[i].equals("+"))){
                num1 = Integer.parseInt(stack.pop());
                if (!stack.isEmpty()){
                    num2 = Integer.parseInt(stack.pop());
                }
                result = num1 + num2;
                stack.push(String.valueOf(result));
            }else if ((!stack.isEmpty()) && (tokens[i].equals("-"))){
                num1 = Integer.parseInt(stack.pop());
                if (!stack.isEmpty()){
                    num2 = Integer.parseInt(stack.pop());
                }
                result = num2 - num1;
                stack.push(String.valueOf(result));
            }else if ((!stack.isEmpty()) && (tokens[i].equals("*"))){
                num1 = Integer.parseInt(stack.pop());
                if (!stack.isEmpty()){
                    num2 = Integer.parseInt(stack.pop());
                }
                result = num2 * num1;
                stack.push(String.valueOf(result));
            }else if ((!stack.isEmpty()) && (tokens[i].equals("/"))){
                num1 = Integer.parseInt(stack.pop());
                if (!stack.isEmpty()){
                    num2 = Integer.parseInt(stack.pop());
                }
                result = num2 / num1;
                stack.push(String.valueOf(result));
            }else {
                stack.push(tokens[i]);
            }
        }
        return Integer.parseInt(stack.peek());
    }
}

代码随想录的代码

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList();
        for (String s : tokens) {
            if ("+".equals(s)) {        // leetcode 内置jdk的问题,不能使用==判断字符串是否相等
                stack.push(stack.pop() + stack.pop());      // 注意 - 和/ 需要特殊处理
            } else if ("-".equals(s)) {
                stack.push(-stack.pop() + stack.pop());
            } else if ("*".equals(s)) {
                stack.push(stack.pop() * stack.pop());
            } else if ("/".equals(s)) {
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2 / temp1);
            } else {
                stack.push(Integer.valueOf(s));
            }
        }
        return stack.pop();
    }
}
注意
  1. 数组的遍历:for (String s : tokens)
  2. 入栈的时候转为int入栈,就不用每次进行四则运算操作时再将string转为int了
  3. 题目不考虑不标准的情况,即不会出现stackpop一次后为空
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值