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. 第8条:覆盖equals时请遵守通用约定

    第8条:覆盖equals时请遵守通用约定 引言:尽管Object是一个具体类,但是设计它主要是为了拓展.它所有的非final方法(equals.hashCode.toString.clone和fina ...

  2. ES6初识-模块化

    export let A=123; export function test(){ console.log('test'); } export class Hello(){ test(){ conso ...

  3. intellij中导入java包

  4. ajax状态值和状态码

    AJAX状态值是指,运行AJAX所经历过的几种状态,无论访问是否成功都将响应的步骤,可以理解成为AJAX运行步骤.如:正在发送,正在响应等,由AJAX对象与服务器交互时所得:使用“ajax.ready ...

  5. 正则表达式re.S的用法

    正则表达式re.S的用法 在Python的正则表达式中,有一个参数为re.S.它表示"."(不包含外侧双引号,下同)的作用扩展到整个字符串,包括"\n".看如下 ...

  6. uniqueidentifier数据类型转换

    cast(id as varchar(36))

  7. Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)

    D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  8. 零基础学html第一天

    html:超文本标记语言 unicode(UTF-8):万国码 <...>:标记标签   :空格 <br>:换行 <hr>:水平线 <p></p& ...

  9. Git Cheatshell - Pro Git

    A git cheatshell based on the book: http://www.git-scm.com/book/en/v2. Repository Configuration git ...

  10. 第四模块:网络编程进阶&数据库开发 练习

    练习题 基于queue模块实现线程池 import threading from multiprocessing import Queue class A(threading.Thread): def ...