算法刷题计划二----无主题随机刷题10(leetCode)

本文介绍了如何使用栈来实现具有最小值查询功能的数据结构,以及如何利用两个栈实现一个队列。在栈的实现中,通过维护一个额外的栈来记录当前栈中的最小值,确保了`push`、`pop`和`getMin`操作的时间复杂度为O(1)。而在队列的实现中,利用入栈和出栈交替操作,当出栈为空时将入栈元素转移,保证了队列的基本操作。这两个数据结构的高效实现展示了对栈和队列特性的巧妙运用。
摘要由CSDN通过智能技术生成

03.02. 栈的最小值

请设计一个栈,除了常规栈支持的pop与push函数以外,还支持min函数,该函数返回栈元素中的最小值。执行push、pop和min操作的时间复杂度必须为O(1)。

示例:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> 返回 -3.
minStack.pop();
minStack.top(); --> 返回 0.
minStack.getMin(); --> 返回 -2.

class MinStack {
    Deque<Integer> s;
    Deque<Integer> m;
    /** initialize your data structure here. */
    public MinStack() {
        s=new LinkedList<Integer>();
        m=new LinkedList<Integer>();
    }
    
    public void push(int x) {
        if(m.isEmpty())
        {
            m.push(x);
        }else
        {
            int y=x<m.peek()?x:m.peek();
            m.push(y);
        }
        s.push(x);
    }
    
    public void pop() {
        s.pop();
        m.pop();
    }
    
    public int top() {
        return s.peek();
    }
    
    public int getMin() {
        return m.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();
 */

个人总结: 发现最大的问题不是做题,而是审题。

03.04. 化栈为队

实现一个MyQueue类,该类用两个栈来实现一个队列。

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

class MyQueue {
    Deque<Integer> instack;
    Deque<Integer> outstack;
    /** Initialize your data structure here. */
    public MyQueue() {
        instack=new LinkedList<Integer>();
        outstack=new LinkedList<Integer>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        instack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        int x=peek();
        outstack.pop();
        return x;
    }
    
    /** Get the front element. */
    public int peek() {
        if(outstack.isEmpty()&&!instack.isEmpty())
        {
            while(!instack.isEmpty())
            {
                outstack.push(instack.peek());
                instack.pop();
            }
        }
        return outstack.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return instack.isEmpty()&&outstack.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();
 */

个人总结: 之前做过,两个栈,只在出栈空时将入栈内容倒入出栈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值