leetcode 232 Implement Queue using Stacks

揭秘Synopsys EDA中的AI黑科技:DSO.ai如何改变传统芯片设计流程 本文深入探讨了Synopsys EDA中的AI黑科技DSO.ai如何革新传统芯片设计流程。通过数据驱动的方法论、功耗优化革命和时序收敛智能加速,DSO.ai显著提升了设计效率和质量,实现了从经验驱动到AI驱动的范式转变。文章还提供了实战指南,帮助团队跨越AI EDA的实施鸿沟。 阅读详情

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.
Notes:
  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.

  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class MyQueue {
    private List<Integer> stack = new ArrayList<Integer>();
    private List<Integer> tmp = new ArrayList<Integer>();

    public void push(int x) {
       stack.add(x);
    }
    public void pop() {
        int len = stack.size();
        tmp.clear();

        for (int i = len-1; i >= 0; i--) {
            tmp.add(stack.get(i));
        }

        stack.clear();

        for (int i = tmp.size()-2; i >= 0; i--) {
            stack.add(tmp.get(i));
        }
    }
    public int peek() {
        int len = stack.size(), ans = -1;

        for (int i = len-1; i>=0; i--) {
            ans = stack.get(i);
        }
        return ans;
    }
    public boolean empty() {
        return stack.size() == 0;
    }
}
public class Solution232 {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        System.out.println(myQueue.peek());
        myQueue.pop();
        myQueue.pop();
        System.out.println(myQueue.empty());
    }
}


STM32定时器ARR寄存器详解:从‘水桶接水’到精准测速,你的PWM和编码器都靠它 本文深入解析STM32定时器ARR寄存器的核心原理与应用技巧,从PWM波形生成到编码器测速实战。通过水桶模型和硬件本质的比喻,揭示ARR如何作为定时器的'心跳节拍器',并详细探讨其在动态调频、死区时间配置和高精度PWM中的关键作用。特别针对编码器模式下的ARR配置和溢出补偿算法进行优化建议,帮助工程师提升系统性能和实时性。 阅读详情

相关推荐

如何用Hugo Stack主题打造极简技术博客?3个必改配置与5个高级美化技巧

本文详细解析了如何深度定制Hugo Stack主题,打造个性化技术博客。文章提供了3个必改配置与5个高级美化技巧,涵盖字体美学、布局重构、代码块视觉强化及细节打磨,帮助开发者超越基础部署,实现从功能到美学的进阶,提升博客质感与用户体验。

carrot的博客 528

leetcode 232:Implement Queue using Stacks

leetcode 232:Implement Queue using Stacks python java c++

西施豆腐渣 8196

“探索thumbdata4文件的奥秘:编程中的神秘存在“

综上所述,thumbdata4文件在编程中是一个常见的存在,与图库应用程序和图片浏览器密切相关。通过使用适当的库和工具,我们可以读取和处理thumbdata4文件中的缩略图数据,以提供更好的用户体验。我们首先读取8字节的块头,其中包含了下一个缩略图块的大小。你可以根据自己的需求来定义对块数据的处理方式,例如将其保存为图像文件或进行其他的分析操作。thumbdata4文件通常以二进制格式存储,其中包含着设备上浏览过的图片的缩略图。因此,你可能需要根据具体的thumbdata4文件结构进行适当的解析和处理。

JieLun_C的博客 772

LeetCode 232Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(

哈哈的个人专栏 5324

LeetCode232 Implement Queue using Stacks Java 题解

题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme

u012249528的专栏 3084

闯关leetcode——232. Implement Queue using Stacks

它要求使用两个栈表达出队列的操作。

方亮的专栏 997

Leetcode_232_Implement Queue using Stacks

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48392363 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop

皮斯特劳沃 2248

LeetCode OJ 232Implement Queue using Stacks

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the

1311

[LeetCode 232] Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(

coder 进阶的专栏 453

[C语言][LeetCode][232]Implement Queue using Stacks

[C语言][LeetCode][232]Implement Queue using Stacks

Timsley的专栏 937

Leetcode 232 Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(

夜色之浓,莫过于黎明前的黑暗。 451

LeetCode232Implement Queue using Stacks

LeetCode232Implement Queue using Stacks

上善若水 572

leetcode】【232Implement Queue using Stacks

一、问题描述 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front el

斗code、年华 346

Leetcode 232. Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(

无名山丘,崛起成峰 1218

LeetCode 232 Implement Queue using Stacks

题目描述Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. empty(

Yano_nankai的博客 596

leetcode——232——Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(

happyxuma1991的博客 220

LeetCode --- 232. Implement Queue using Stacks

232. Implement Queue using Stacks Difficulty: Easy Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front...

MissXy_的博客 383

LeetCode232Implement Queue using Stacks

class MyQueue { private: stack&lt;int&gt; sta; stack&lt;int&gt; sta1; public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the...

weixin_39458342的博客 83

LeetCode 232. Implement Queue using Stacks 题解

本题是栈和队列的经典应用问题,主要考察对栈和队列数据结构的理解和使用。通过使用两个栈,我们可以高效地实现队列的所有操作。双栈的核心思想是:使用两个栈,一个输入栈用于存储新元素,一个输出栈用于存储待出队的元素,当需要出队或查看队首元素时,如果输出栈为空,将输入栈的所有元素转移到输出栈。这种方法不仅适用于用栈实现队列问题,还可以应用于许多其他需要栈和队列转换的问题。掌握双栈的使用,对于解决这类问题非常重要。

cannonjinx的博客 2524

LeetCode 232. Implement Queue using Stacks

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-232-implement-queue-using-stacks/ 解答[Java] import java.util.ArrayDeque; import java.util.Deque; class MyQueue { Deque<Integer> stack1; ...

user2639的博客 180

(52页PPT)基于PLM的数字化工厂解决方案.pptx

(52页PPT)基于PLM的数字化工厂解决方案.pptx

解决windows10下无法安装.net framework 3.5(包括.net2.0和3.0)

.sxs

上一篇: Leetcode233 Number of Digit One
下一篇: Leetcode231 Power of Two
walter1990
博客等级 码龄15年 31粉丝 376原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值