# LeetCode Stack Design专题

LeetCode Stack / Design专题专题部分，更多说明请见LeetCode Array专题

## 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() – 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).

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

### 实现

public class TwoQueueUsingStacks {

Stack<Integer> stackA = new Stack<>();
Stack<Integer> stackB = new Stack<>();

// Push element x to the back of queue.
public void push(int x) {
stackA.push(x);
}

// Removes the element from in front of queue.
public void pop() {
if (stackB.empty()) {
while (!stackA.isEmpty()) {
stackB.push(stackA.pop());
}
}
stackB.pop();
}

// Get the front element.
public int peek() {
if (stackB.empty()) {
while (!stackA.isEmpty()) {
stackB.push(stackA.pop());
}
}
return stackB.peek();
}

// Return whether the queue is empty.
public boolean empty() {
return (stackA.empty()) && (stackB.empty());
}
}

## 225. Implement Stack using Queues

Implement the following operations of a stack using queues.

• push(x) – Push element x onto stack.
• pop() – Removes the element on top of the stack.
• top() – Get the top element.
• empty() – Return whether the stack is empty.

Notes:

• You must use only standard operations of a queue – which means only push to backpeek/pop from frontsize, and is empty operations are valid.
• Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
• You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

• push(x) – Push element x onto stack.
• pop() – Removes the element on top of the stack.
• top() – Get the top element.
• empty() – Return whether the stack is empty.

### 实现

#### 方法一：队列A负责入栈，队列B负责中转A中的元素

class MyStack {

// Push element x onto stack.
public void push(int x) {
if (queueB.isEmpty()) {
} else {
}
}

// Removes the element on top of the stack.
public void pop() {
if (queueA.isEmpty() && queueB.isEmpty()) {
return;
}
if (queueB.isEmpty()) {
while(queueA.size() > 1) {
}
if (queueA.size() > 0) {
queueA.remove();
}
} else {
while(queueB.size() > 1) {
}
if (queueB.size() > 0) {
queueB.remove();
}
}
}

// Get the top element.
public int top() {
if(queueA.isEmpty() && queueB.isEmpty()) {
return 0;
}

if (queueB.isEmpty()) {
while(queueA.size() > 1) {
}
int lastElement = queueA.peek();
return lastElement;
} else {
while(queueB.size() > 1) {
}
int lastElement = queueB.peek();
return lastElement;
}
}

// Return whether the stack is empty.
public boolean empty() {
return (queueA.isEmpty() && queueB.isEmpty());
}
}

## 155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

• push(x) – Push element x onto stack.
• pop() – Removes the element on top of the stack.
• top() – Get the top element.
• getMin() – Retrieve the minimum element in the stack.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();   --> Returns -3.
minStack.pop();
minStack.top();      --> Returns 0.
minStack.getMin();   --> Returns -2.

### 实现

public class MinStack {

Stack<Integer> minValueStack = new Stack<>();
Stack<Integer> mStack = new Stack<>();

public void push(int x) {
mStack.push(x);
if (minValueStack.isEmpty() || minValueStack.peek() >= x) {
minValueStack.push(x);
}
}

public void pop() {
if (mStack.isEmpty()) return;
else {
if (!minValueStack.isEmpty()) {
if (mStack.peek().equals(minValueStack.peek())) {
minValueStack.pop();
}
}
mStack.pop();
}
}

public int top() {
if (!mStack.isEmpty()) {
return mStack.peek();
} else return 0;
}

public int getMin() {
if (minValueStack.isEmpty()) return 0;
else return minValueStack.peek();
}
}

### 问题分析

#### 【LeetCode】Min Stack 解题报告

2014-11-10 16:17:37

#### LEETCODE 155

2016-03-10 17:08:02

#### LeetCode-Min Stack(包含min函数的栈)

2015-02-05 11:18:43

#### LeetCode 155. Min Stack（最小栈）

2016-05-26 00:56:06

#### LeetCode之Stack题目汇总

2015-12-29 16:58:31

#### LeetCode刷题 (Python) | 155. Min Stack

2016-01-30 17:53:12

#### LeetCode 155 : Min Stack (Java)

2015-12-12 14:58:09

#### 【LeetCode-面试算法经典-Java实现】【155-Min Stack（最小栈）】

2015-08-21 06:30:33

#### leetcode 专题—sort

2015-05-06 19:26:34

#### leetcode 225: Implement Stack using Queues

2015-06-19 01:00:54