Question:

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).

Analysis:

问题描述:用队列模仿一个栈。

思路:用两个队列模仿一个栈。每次要pop或者peek时,使用队列倒换一下,剩下最后一个元素单独处理。当且仅当两个队列都为空时,栈才为空。

Answer:

class MyStack {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>(); // Push element x onto stack.
public void push(int x) {
q1.offer(x);
} // Removes the element on top of the stack.
public void pop() {
if(!q1.isEmpty()) {
while(q1.size() > 1) {
int i = q1.poll();
q2.offer(i);
}
q1.poll();
} else {
while(q2.size() > 1) {
int i = q2.poll();
q1.offer(i);
}
q2.poll();
}
} // Get the top element.
public int top() {
if(!q1.isEmpty()) {
while(q1.size() > 1) {
int i = q1.poll();
q2.offer(i);
}
int i = q1.poll();
q2.offer(i);
return i;
} else {
while(q2.size() > 1) {
int i = q2.poll();
q1.offer(i);
}
int i = q2.poll();
q1.offer(i);
return i;
}
} // Return whether the stack is empty.
public boolean empty() {
if(q1.size() == 0 && q2.size() == 0)
return true;
return false;
}
}

LeetCode -- Implement Stacks using Queue的更多相关文章

  1. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. LeetCode——Implement Queue using Stacks

    Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...

  4. LeetCode Implement Queue using Stacks (数据结构)

    题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...

  5. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  6. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  7. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

随机推荐

  1. sqlite3基本操作

    在移动设备上进行高性能高效率的大量数据存储,我们一般用得时sqlite这款轻巧型的数据库,这里介绍其增删改查基本功能 在ios开发中我们需要先导入"libsqlite3.dylib" ...

  2. 【ODT】cf896C - Willem, Chtholly and Seniorious

    仿佛没用过std::set Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is ...

  3. linux特殊权限位suid

    特殊权限位基本说明(了解): linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限: suid       s(x)     S     4     用户对应的权限位(用户对应的3位 ...

  4. Python函数及参数

    ## 函数 - 函数是代码的一种组织形式,一般一个函数完成一个特定功能 - 函数需要先定义后使用 - 函数的定义 def func_name(参数): func_body ... return fun ...

  5. MYSQL SQL高级查询技巧

    1.UNION,EXCEPT,INTERSECT运算符 A,UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表. ...

  6. 超简单开发自己的php框架一点都不难

    (转)https://blog.csdn.net/qq_33862644/article/details/79344331 写框架的极简思路: 接收,打印参数想怎么弄.如 获取配置文件的方法,根据传过 ...

  7. Python学习第一弹

    开发语言: 高级:Python.java.PHP  C#   GO  ruby   C++           ——>字节码   低级:C.汇编                          ...

  8. Personal Collection

    1.常用网站 序号 网址 标题 1 https://www.oschina.net/ 开源软件 2 http://tool.oschina.net/ 开发常用工具网站 3 https://docs.o ...

  9. HDU 4405 Aeroplane chess(期望dp)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. JWT应用

    调试器库简介问一件T恤! 精心制作 JSON Web令牌简介 新:免费获得JWT手册并深入学习JWT! 什么是JSON Web Token? JSON Web Token(JWT)是一个开放标准(RF ...