【数据结构】栈和队列的基本应用

  1. 括号匹配问题
class Solution {
    public boolean isValid(String s) {
        if (s.length() == 0)
            return true;
        if ((s.length() & 1) == 1)
            return false;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            switch (s.charAt(i)) {
                case '(':
                case '[':
                case '{':
                    stack.push(s.charAt(i));
                    continue;
                case ')':
                    if (stack.isEmpty() || stack.pop() != '(')
                        return false;
                    continue;
                case ']':
                    if (stack.isEmpty() || stack.pop() != '[')
                        return false;
                    continue;
                case '}':
                    if (stack.isEmpty() || stack.pop() != '{')
                        return false;
                    continue;
            }
        }
        return stack.isEmpty();

    }
}
  1. 用队列实现栈
class MyStack {
 Queue<Integer> queue;
    /** Initialize your data structure here. */
    public MyStack() {
      queue = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
       queue.offer(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
       int size = queue.size();
        for (int i = 0; i < size - 1; i++) {
            queue.offer(queue.poll());
        }
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
         int size = queue.size();
        for (int i = 0; i < size - 1; i++) {
            queue.offer(queue.poll());
        }
        int result = queue.poll();
        queue.offer(result);
        return result;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
     return queue.isEmpty();
    }
}
  1. 用栈实现队列
class MyQueue {
 Stack s1;
    Stack s2;
    public MyQueue() {
         this.s1 = new Stack();
        this.s2 = new Stack();
    }
    
    public void push(int x) {
         s1.push(x);
    }

    public int pop() {
         if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return (int) s2.pop();
    }
    
    public int peek() {
         if (s2.empty()) {
            while (!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return (int) s2.peek();
    }
 
    public boolean empty() {
          return s1.empty() && s2.isEmpty();
    }
}
  1. 实现一个最小栈
    (定义一个正常栈,一个最小栈,一起出栈)
class MinStack {
Stack<Integer> stack1;
    Stack<Integer> stack2;
    /** initialize your data structure here. */
    public MinStack() {
         stack1=new Stack<Integer>();
        stack2=new Stack<Integer>();
    }
    
    public void push(int x) {
         stack1.push(x);
       if(stack2.isEmpty()){
           stack2.push(x);
       }else if(stack2.peek()<x){
           stack2.push(stack2.peek());
       }else{
           stack2.push(x);
       }


    }
    
    public void pop() {
         stack1.pop();
        stack2.pop();
    }
    
    public int top() {
        return stack1.peek();
    }
    
    public int getMin() {
        return stack2.peek();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值