算法-栈-20230506

1.用栈实现队列

class MyQueue {

    private Stack<Integer> stackIn = new Stack<>();
    private Stack<Integer> stackOut = new Stack<>();


    public MyQueue() {

    }
    
     public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        if (stackOut.isEmpty()) {
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
        }
        return stackOut.pop();
    }
    
    public int peek() {
        if (stackOut.isEmpty()) {
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
        }
        return stackOut.peek();
    }
    
    public boolean empty() {
        return stackOut.isEmpty() && stackIn.isEmpty();
    }
}

2 用队列实现栈,

2.1 复杂度比较高,其实和使用一个队列效果一样,没有发挥两个队列的优势

class MyStack {
    Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();

    public MyStack() {

    }
    
    public void push(int x) {
        queue1.offer(x);
    }
    
    public int pop() {
        int poll = copy1to2();
        copy2to1();
        return poll;
    }
    
    public int top() {
        int poll = copy1to2();
        copy2to1();
        queue1.offer(poll);
        return poll;
    }

    private int copy1to2() {
        Integer poll = 0;
        while (!queue1.isEmpty()) {
            poll = queue1.poll();
            if (!queue1.isEmpty()) {
                queue2.offer(poll);
            }
        }
        return poll;
    }

    private void copy2to1() {
        while (!queue2.isEmpty()) {
            Integer poll = queue2.poll();
            queue1.offer(poll);
        }
    }
    
    public boolean empty() {
        return queue1.isEmpty();
    }
}

2.1 使用两个栈,一个保存逆序的队列

Queue<Integer> queue1 = new LinkedList<>();
    Queue<Integer> queue2 = new LinkedList<>();

    public MyStack() {

    }
    
    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        // 交换
        Queue<Integer> queue = queue1;
        queue1 =queue2;
        queue2 = queue;
    }
    
    public int pop() {
        return queue1.poll();
    }
    
    public int top() {
        return queue1.peek();
    }
    
    public boolean empty() {
        return queue1.isEmpty();
    }

3.括号有效

// 栈
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (stack.size() > 0) {
                if (stack.peek() == '(' && s.charAt(i) == ')' ||
                        stack.peek() == '{' && s.charAt(i) == '}' ||
                        stack.peek() == '[' && s.charAt(i) == ']') {
                    stack.pop();
                    continue;
                }
            }
            stack.push(s.charAt(i));
        }
        return stack.isEmpty();

4. 消消乐-删除字符

Stack<Character> stack = new Stack<>();
        for (int i =0; i< s.length(); i++) {
            if (!stack.isEmpty()) {
                if (stack.peek() == s.charAt(i)) {
                    stack.pop();
                    continue;
                }
            }
            stack.push(s.charAt(i));
        }
        String str = "";
        while (!stack.isEmpty()) {
            str = stack.pop() + str;
        }
        return str;

5. 逆波兰表达式

public int evalRPN(String[] tokens) {
        Stack<Integer> stackNum = new Stack<Integer>();
        for (String token : tokens){
            if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
                if (!stackNum.isEmpty()) {
                    Integer num2 = stackNum.pop();
                    Integer num1 = stackNum.pop();
                    if (token.equals("+")) {
                        stackNum.push(num1 + num2);
                    }
                    if (token.equals("-")) {
                        stackNum.push(num1 - num2);
                    }
                    if (token.equals("*")) {
                        stackNum.push(num1 * num2);
                    }
                    if (token.equals("/")) {
                        stackNum.push(num1 / num2);
                    }
                }
            }else {
                stackNum.push(Integer.valueOf(token));
            }
        }
        return stackNum.pop();
    }

6. 滑动窗口内最大值

6.1 队列,用了半天栈,超时。还是双向队列好用

public int[] maxSlidingWindow(int[] nums, int k) {
        int[] result = new int[nums.length - k + 1];
        int index = 0;
        LinkedList<Integer> stack = new LinkedList<>();
        while (index < k) {
            pushNext(stack, nums[index++]);
        }
        result[0] = stack.peek();
        for (int i = 1, right = i+k-1; i < result.length; i++,right++) {
            if (stack.peek() ==nums[i-1]) {
                stack.pop();
            }
            pushNext(stack, nums[right]);
            result[i] = stack.peek();
        }
        return result;
    }

    private void pushNext(LinkedList<Integer> linkedList, int value) {
        while (!linkedList.isEmpty() && linkedList.getLast() < value) {
            linkedList.removeLast();
        }
        linkedList.addLast(value);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值