小白视角刷leetcode(232. 用栈实现队列、225. 用队列实现栈)

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
​
实现 MyQueue 类:
​
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
思路:
    1.栈是先进后出,队列是先进先出,因此要保证相同顺序输出,需要新建两个栈,“输入栈”和“输出栈”,并将输入栈的内容重新输出到输出栈,为保证顺序不能出错,需要先对输出栈是否非空进行判断,如果输出栈不为空则直接return。输出栈为空时利用while循环将输入栈的内容全部加入到输出栈中
    2.然后再看题目要求四个功能实现,push不需要改动,pop,peek则需要对输入栈和输出栈进行颠倒操作后,可以直接操作,isempty直接用&&连接并返回即可
class MyQueue {
        Stack<Integer> StackIn ;
        Stack<Integer> StackOut ;
    public MyQueue() {
         StackIn = new Stack<>();
         StackOut = new Stack<>();
    }
    
    public void push(int x) {   
         StackIn.push(x);
    }
    
    public int pop() {
        TrandQueue();
         return StackOut.pop();
    }
    
    public int peek() {
        TrandQueue();
        return StackOut.peek();
    }
    
    public boolean empty() {
        return StackOut.isEmpty() && StackIn.isEmpty();
    }
    public void TrandQueue(){
        if(!StackOut.isEmpty()) return ;
        while(!StackIn.isEmpty()){
            StackOut.push(StackIn.pop());
        }
    }
}
​
/**
 * 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();
 */

225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
​
实现 MyStack 类:
​
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
思路:
    1.如果想用一个队列实现栈,需要在每次插入元素的时候,都将前size-1个元素分别重新加入队列,以此保证新加入的队列在最前边。
    2.也可以想用栈实现队列那样,单独新建一个函数,但比这种方法复杂
class MyStack {
    Queue<Integer> query;
    public MyStack() {
        query = new LinkedList<>();
    }
    
    public void push(int x) {
        query.offer(x);
        int size = query.size();
        for(int i = 0 ; i < query.size()-1 ;i++){
            query.add(query.poll());
        }
    }
    
    public int pop() {
        return query.poll();
    }
    
    public int top() {
        return query.peek();
    }
    
    public boolean empty() {
        return query.isEmpty();
    }
    // public void dumpBack(int x){
    //     int size = query.size();
    //     for(int i = 0 ; i < query.size()-1 ;i++){
    //         query.add(query.poll(x));
    //     }
    // }
}
​
/**
 * 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();
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘成长日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值