栈、队列、堆随笔

1/ Leetcode 225 使用队列实现栈

1. 队列的初始化: Queue是接口,队列由链表实现 : Queue<> q = new LinkedList<>();

2.Queue的基本使用方法:

  • offer        添加一个元素并返回true        如果队列已满,则返回false   
  • poll          移除并返问队列头部的元素    如果队列为空,则返回null   
  • peek        返回队列头部的元素              如果队列为空,则返回null 
class MyStack {
    private Queue<Integer> a;
    private Queue<Integer> b;

    /** Initialize your data structure here. */
    public MyStack() {
        a = new LinkedList<Integer>();
        b = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        b.offer(x);
        while(!a.isEmpty())
        {
            b.offer(a.poll());
        }

        Queue<Integer> temp;
        temp = b;
        b = a;
        a = temp;
        
   }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return a.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return a.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return a.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();
 */

2/Leetcode 232用栈实现队列

1.Stackh是类 直接初始化

2.基本操作

* push : 把项压入堆栈顶部 ,并作为此函数的值返回该对象
* pop : 移除堆栈顶部的对象,并作为此函数的值返回该对象
* peek : 查看堆栈顶部的对象,,并作为此函数的值返回该对象,但不从堆栈中移除它
* empty : 测试堆栈是否为空

class MyQueue {

    private Stack<Integer> stack;
    private Stack<Integer> a;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack = new Stack<>();
        a = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        while(!stack.empty())
        {
            a.push(stack.pop());
        }
        stack.push(x);
        while(!a.empty())
        {
            stack.push(a.pop());
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        return stack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.empty();
    }
}

/**
 * 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();
 */

3/Leetcode 255 设计最小栈

思路:设计一个同步的对照栈,记录栈所有状态下的最小值。

class MinStack {

    Stack<Integer> stack;
    Stack<Integer> stackmin;
    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<>();
        stackmin = new Stack<>();
    }
    
    public void push(int x) {
        stack.push(x);
        if(!stackmin.empty())
        {
            if(stackmin.peek()<x)
            {
                stackmin.push(stackmin.peek());
            }
            else
            {
                stackmin.push(x);
            }
        }
        else 
        {
            stackmin.push(x);
        }
    }
    
    public void pop() {
        stack.pop();
        stackmin.pop();

    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return stackmin.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/Leetcode 946 验证栈序列

1. 构造辅助栈。将pushed序列推入栈中,比较栈顶元素与popped数组。相同时弹出

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> s = new Stack<>();
        int index = 0;
        for(int i=0;i<pushed.length;i++)
        {
            s.push(pushed[i]);
            while(!s.empty()&&s.peek()==popped[index])
            {
                index++;
                s.pop();
            }
        }
        if(s.empty())
        {
            return true;
        }
        else return false;
    }
}

4/Leetcode 224 基本计算器 经典的栈应用

。。。。。好难md

 

5/Leetcode 215  数组中第K个最大元素

快排

 

posted on 2020-04-15 00:04  洛小侠  阅读(126)  评论(0编辑  收藏  举报

导航