Leedcode算法专题训练(栈和队列)

1. 用栈实现队列

232. Implement Queue using Stacks (Easy)

Leetcode / 力扣

class MyQueue {
    Stack<Integer> stack1=new Stack<>();
    Stack<Integer> stack2=new Stack<>();

    /** Initialize your data structure here. */
    public MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop()); 
            } 
        }
        return stack2.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop()); 
            } 
        }
        
        return stack2.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty()&&stack2.isEmpty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

2. 用队列实现栈

225. Implement Stack using Queues (Easy)

Leetcode / 力扣

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) {
        int n=queue.size();
        queue.add(x);
        while(n-->0){
            queue.add(queue.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

3. 最小值栈

155. Min Stack (Easy)

Leetcode / 力扣

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    /** initialize your data structure here. */
    public MinStack() {
        stack=new Stack<>();
        minStack=new Stack<>();
    }
    
    public void push(int x) {
        stack.push(x);
        if(minStack.isEmpty()||minStack.peek()>=x){
            minStack.add(x);
        }
    }
    
    public void pop() {
        int val=stack.pop();
        if(val==minStack.peek())minStack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

4. 用栈实现括号匹配

20. Valid Parentheses (Easy)

Leetcode / 力扣

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        for(char c: s.toCharArray()){
            if(c=='(')stack.push(')');
            else if(c=='[')stack.push(']');
            else if(c=='{')stack.push('}');
            else if(stack.isEmpty()||stack.pop()!=c)return false;
        }
        return stack.isEmpty();
    }
}

5. 数组中元素与下一个比它大的元素之间的距离

这个题目很巧妙,主要的用法在于是对栈存的元素是索引,并且是个单调栈。

739. Daily Temperatures (Medium)

Leetcode / 力扣

class Solution {
    public int[] dailyTemperatures(int[] T) {
        Stack<Integer> stack=new Stack<>();
        int N=T.length;
        int[] result=new int[N];
        for(int i=0;i<N;i++){ 
            while(!stack.isEmpty()&&T[i]>T[stack.peek()]){
                int temp=stack.pop();
                result[temp]=i-temp;
            }
            stack.push(i);
        }
        return result;

    }
}

6. 循环数组中比当前元素大的下一个元素'

也就是多了几行代码,全部设置为-1,循环的化用俩个数组

  • 思路
  • 1.将数组中所有元素全部置为-1
  • 2.遍历两次,相当于循环遍历
  • 3.第一遍遍历,入栈索引i
  • 4.只要后面元素比栈顶索引对应的元素大,索引出栈,更改res[sta.pop()]的数值
  • 5.最后栈里面剩余的索引对应的数组值,都为默认的-1(因为后面未找到比它大的值) */

503. Next Greater Element II (Medium)

Leetcode / 力扣

class Solution {
    public int[] nextGreaterElements(int[] nums) {
        int N=nums.length;
        int[] res=new int[N];
        Arrays.fill(res,-1);
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<N*2;i++){
            int num=nums[i%N];
            while(!stack.isEmpty()&&num>nums[stack.peek()]){
                res[stack.pop()]=num;
            }
            if(i<N)stack.push(i);
        }
        return res;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值