LeetCode每日1题--用栈实现队列

简介: LeetCode每日1题--用栈实现队列

前言


算法的重要性不言而喻!区分度高!

现在学习的门槛低了,只有能上网每个人都可以学编程!培训班6个月就可以培养出来能干活的人,你怎么从这些人中脱颖而出?没错!就是学算法,学一些底层和基础的东西。

说的功利点是为了竞争,卷死对手。真心话说就是能提高自己的基础能力,为技术可持续发展做好充分的准备!!!

提前入门学习书籍:CPrimerPlus、大话数据结构

image.png


刷题网站


代码随想录 (programmercarl.com)

leetcode

我是按照代码随想录提供的刷题顺序进行刷题的,大家也可以去刷leetcode最热200道,都可以

刷题嘛,最重要的就是坚持了!!!


画图软件


OneNote

这个要经常用,遇见不懂的流程的话就拿它画一画!


笔记软件


Typoral


题目


image.png

看到简单题,我上去就是重拳出击!!!拿下!


解析


首先我们要知道栈和队列的特征:

  • 栈:先进后出
  • 队列:先进先出

ok,那么怎么用去模拟呢?栈和队列元素进入的顺序是一样的,就是出的顺序不一样。所以我们在用一个队列就能模拟出和队列一样的弹出元素的顺序了!

动画模拟以下队列的执行过程如下:

执行语句: queue.push(1); queue.push(2); queue.pop(); 注意此时的输出栈的操作 queue.push(3); queue.push(4); queue.pop(); queue.pop();注意此时的输出栈的操作 queue.pop(); queue.empty();

这个网站写的是真的好,强烈推荐大家去学习!

首先初始化栈


public MyQueue() {
    stackIn = new Stack<>(); // 负责进栈
    stackOut = new Stack<>(); // 负责出栈
  }

从栈中加入元素

push可以想象为放入的意思

public void push(int x) {
    stackIn.push(x)

从栈中删除元素

pop可以想象成弹出的意思

public int pop() {    
dumpstackIn();
return stackOut.pop();
}

得到栈首元素

peek是偷看的意思,程序中就是得到队列的首元素

public int peek() {
  dumpstackIn();
  return stackOut.peek();
}

返回你的栈是否为空


public boolean empty() {
  return stackIn.isEmpty() && stackOut.isEmpty();
}

OK,接下来就是元素进入第二个栈,也就是模拟队列的出栈过程

什么意思呢?就是如果第一个栈为空了,那么就把元素放到另一个栈中

private void dumpstackIn(){
    if (!stackOut.isEmpty()) return; 
    while (!stackIn.isEmpty()){
      stackOut.push(stackIn.pop());
    }
  }

完整代码如下:

class MyQueue {
  Stack<Integer> stackIn;
  Stack<Integer> stackOut;
  /** Initialize your data structure here. */
  public MyQueue() {
    stackIn = new Stack<>(); // 负责进栈
    stackOut = new Stack<>(); // 负责出栈
  }
  /** Push element x to the back of queue. */
  public void push(int x) {
    stackIn.push(x);
  }
  /** Removes the element from in front of queue and returns that element. */
  public int pop() {    
    dumpstackIn();
    return stackOut.pop();
  }
  /** Get the front element. */
  public int peek() {
    dumpstackIn();
    return stackOut.peek();
  }
  /** Returns whether the queue is empty. */
  public boolean empty() {
    return stackIn.isEmpty() && stackOut.isEmpty();
  }
  // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
  private void dumpstackIn(){
    if (!stackOut.isEmpty()) return; 
    while (!stackIn.isEmpty()){
      stackOut.push(stackIn.pop());
    }
  }
}

总结

没错,就是这么简单 明天继续



相关文章
LeetCode 热题100——单调栈
LeetCode 热题100——单调栈
19 0
|
2月前
|
Go C++
【力扣】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
【2月更文挑战第18天】2696. 删除子串后的字符串最小长度(模拟 栈 C++ Go实现栈)
34 6
|
4月前
【Leetcode 2487】从链表中移除节点 —— 单调栈
解题思路:维持一个单调递增栈,当栈为空时,记录当前节点为头节点;否则当前节点为栈顶节点的后继节点
|
4月前
【Leetcode 1944】队列中可以看到的人数 —— 单调栈
解题思路:维持一个单调递增栈来辅助计算每个人能够看到的人数。从右往左遍历数组,对于每个人,我们将其身高与栈顶元素的身高进行比较。如果当前人的身高比栈顶元素的身高高,则栈顶元素无法被当前人看到,将其出栈,并累计计数
LeetCode | 232. 用栈实现队列
LeetCode | 232. 用栈实现队列
|
5天前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
5 0
|
27天前
|
C语言
Leetcode每日一题——“用栈实现队列”
Leetcode每日一题——“用栈实现队列”
|
27天前
|
C语言
Leetcode每日一题——“用队列实现栈”
Leetcode每日一题——“用队列实现栈”
|
2月前
|
存储
leetcode1944. 队列中可以看到的人数
leetcode1944. 队列中可以看到的人数
16 0
|
2月前
|
算法 安全 Java
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
【数据结构与算法】6、栈(Stack)的实现、LeetCode:有效的括号
22 0