【专项刷题】— 栈

 1、删除字符串中的所有相邻重复项 - 力扣(LeetCode)

思路:

  1. 使用栈进行操作,每次入栈的时候和栈顶元素进行比对,如果相同的话就弹出栈顶元素
  2. 也可以用数组来模拟栈进行操作
  3. 代码:
    public String removeDuplicates(String s) {
            //转换成字符数组
            char[] ss = s.toCharArray();
            StringBuffer ret = new StringBuffer();
            for(char ch : ss){
                //当长度不为0且最后一个字符和要加入的字符相等的时候
                if(ret.length() > 0 && ret.charAt(ret.length()-1) == ch){
                    //删除
                    ret.deleteCharAt(ret.length() - 1);
                }else{
                    //否则添加
                    ret.append(ch);
                }
            }
            return ret.toString();
        }

 2、基本计算器 II - 力扣(LeetCode)

思路:

代码:

public int calculate(String ss) {
        char[] s = ss.toCharArray();
        Deque<Integer> st = new ArrayDeque<>();
        int n = s.length;
        char op = '+';
        int i = 0;
        while(i < n){
            //排除空格
            if(s[i] == ' '){
                i++;
            }else if(s[i] >= '0' && s[i] <= '9'){
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9'){//先更新数值
                    tmp = tmp*10 + (s[i] - '0');
                    i++;
                }
                //根据当前的操作符进行入栈
                if(op == '+'){
                    st.push(tmp);
                }else if(op == '-'){
                    st.push(-tmp);
                }else if(op == '*'){
                    st.push(st.poll() * tmp);
                }else{
                    st.push(st.poll() / tmp);
                }
            }else{
                //更新操作符
                op = s[i];
                i++;
            }
        }
        //出栈相加
        int ret = 0;
        while(!st.isEmpty()){
            ret += st.poll();
        }
        return ret;
    }

3、字符串解码 - 力扣(LeetCode)

思路:

代码:

public String decodeString(String ss) {
        char[] s = ss.toCharArray();
        int n = s.length;
        Stack<StringBuffer> str = new Stack<>();
        str.push(new StringBuffer());//先放一个空串
        Stack<Integer> nums = new Stack<>();
        int i = 0;
        while(i < n){
            if(s[i] >= '0' && s[i] <= '9'){
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9'){
                    tmp = tmp*10 + (s[i] - '0');
                    i++;
                }
                nums.push(tmp);
            }else if(s[i] == '['){
                //先跳过 [ ,提取后面的字符
                i++;
                StringBuffer tmp = new StringBuffer();
                while(i < n && s[i] >= 'a' && s[i] <= 'z'){
                    tmp.append(s[i]);
                    i++;
                }
                str.push(tmp);
            }else if(s[i] == ']'){
                //开始解析,
                StringBuffer tmp = str.pop();
                int k = nums.pop();
                while(k-- != 0){
                    str.peek().append(tmp);
                }
                i++;
            }else{
                //处理纯字符的情况,直接加到栈顶字符的后面
                StringBuffer tmp = new StringBuffer();
                while(i < n && s[i] >= 'a' && s[i] <= 'z'){
                    tmp.append(s[i]);
                    i++;
                }
                str.peek().append(tmp);
            }
        }
        return str.peek().toString();
    }

 4、验证栈序列 - 力扣(LeetCode)

思路:

public boolean validateStackSequences(int[] pushed, int[] popped) {
        int j = 0;
        Deque<Integer> s = new ArrayDeque<>();
        //遍入栈的数组
        for(int x : pushed){
            //先入栈
            s.push(x);
            //如果栈顶与出栈顺序的数组相等
            while(!s.isEmpty() && s.peek() == popped[j]){
                s.pop();
                j++;
            }
        }
        return j == pushed.length;
    }

数组模拟栈:

public boolean validateStackSequences(int[] pushed, int[] popped) {
        int index = 0;
        int[] tmp = new int[pushed.length];
        for(int i = 0, j = 0; i < pushed.length; i++){
            tmp[index++] = pushed[i];
            while(index > 0 && tmp[index - 1] == popped[j]){
                index--;
                j++;
            }
        }
        return index == 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值