day11算法补卡|栈与队列02|Leetcode20有效括号、1047删除字符串中的所有相邻重复项 、150逆波兰表达式求值

Leetcode20:有效括号

题目链接:https://leetcode.cn/problems/valid-parentheses/description/

 题目分析:使用栈实现,如果栈为空,直接入栈;如果栈不为空且栈顶元素与即将遍历字符匹配,则对应栈顶元素出栈;如不匹配,则对元素直接入栈

Java实现代码:


import java.util.Stack;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean isValid(String s) {
        int length=s.length();
        Stack<Character> strstack = new Stack<>();
        for (int i = 0; i < length; i++) {
            char thechar = s.charAt(i);
            if(strstack.isEmpty()){
                strstack.push(thechar);
                continue;
            }
            char stacktop = strstack.peek();
            if((thechar == ')' && stacktop == '(')||(thechar == '}' && stacktop == '{')||(thechar == ']' && stacktop == '[')){
                strstack.pop();
            }else {
                strstack.push(thechar);
            }
        }
        return strstack.isEmpty();
    }
}
//leetcode submit region end(Prohibit modification and deletion)

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

题目链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/

题目分析:与括号匹配思路一致 均属于消消消问题

Java实现代码:

class Solution {
    public String removeDuplicates(String s) {
        int leng = s.length();
        Stack<Character> strstack = new Stack<>();
        for (int i = 0; i < leng; i++) {
            char thechar = s.charAt(i);
            if(strstack.isEmpty()){
                strstack.push(thechar);
                continue;
            }
            if(strstack.peek() == thechar){
                strstack.pop();
            }else {
                strstack.push(thechar);
            }
        }
        String returnstr = "";
        while (!strstack.isEmpty()){
            returnstr = strstack.pop()+returnstr;
        }

        return returnstr;
    }
}

Leetcode150.逆波兰表达式求值

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

主要是'逆向'--要注意其中的除法与减法计算

主要思路是:遇到符号---两次pop 运算后进行依次push;不是符号直接push

Java实现代码:

import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> strstack = new Stack<>();
        int size = tokens.length;
        for (int i =0;i<size;i++){
            String thestr = tokens[i];
            if(thestr.equals("+")){
                strstack.push(strstack.pop()+strstack.pop());
            }else if(thestr.equals("-")){
                strstack.push(-strstack.pop()+strstack.pop());
            }else if(thestr.equals("*")){
                strstack.push(strstack.pop()*strstack.pop());
            }else if(thestr.equals("/")){
                int numA=strstack.pop();
                int numB=strstack.pop();
                strstack.push(numB/numA);
            }else{
                strstack.push(Integer.valueOf(thestr));
            }

        }
        return strstack.pop();
    }
}

_____________________________________________________________________________

今日补卡结束:对于栈与队列相关问题,主要是把握其'消消消'特性与顺序问题,想清楚能够使用时就很好写出题解;

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值