高频面试题!
1.用队列实现栈
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues
//定义两个队列实现栈
Queue<Integer> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
public MyStack() {
}
//模拟入栈
//思想:在不为空的队列对元素入队,然后把size-1个元素依次出队放到另外一个队列中
public void push(int x) {
if(!queue1.isEmpty()){
queue1.offer(x);
}else if(!queue2.isEmpty()){
queue2.offer(x);
}else{
queue1.offer(x);
}
}
//模拟入栈
//每次出不为空的队列,出size-1个 到另外一个为空的队列,最后弹出剩余的那一个元素
public int pop() {
if(empty()){
return -1;
}
int count = 0;
if(!queue1.isEmpty()){
int size = queue1.size();
while(count != size-1){
queue2.offer(queue1.poll());
count++;
}
return queue1.poll();
}else{
int size = queue2.size();
while(count != size-1){
queue1.offer(queue2.poll());
count++;
}
return queue2.poll();
}
}
//获取栈顶元素
public int top() {
if(empty()){
return -1;
}
int cur = 0; //给个局部变量为了方便表示栈顶元素
if(!queue1.isEmpty()){
int size = queue1.size();
for(int i =0; i < size; i++){
cur = queue1.poll();
queue2.offer(cur);
}
return cur; //得到栈顶元素
}else{
int size = queue2.size();
for(int i =0; i < size; i++){
cur = queue2.poll();
queue1.offer(cur);
}
return cur;
}
}
//判空
public boolean empty() {
//return queue1.isEmpty() && queue2.isEmpty();
if(queue1.isEmpty() && queue2.isEmpty()){
return true;
}
return false;
}
2.用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks
//双栈实现队列
Stack<Integer> stack1 = new Stack<>(); //当做入队角色
Stack<Integer> stack2 = new Stack<>(); //当做出队角色
public MyQueue() {
}
//模拟入队
public void push(int x) {
stack1.push(x); //将元素压到栈stack1
}
//拿到队列开头元素并删除
public int pop() {
if(empty()){
return -1;
}
//当stack2为空时,遍历stack1,将stack1元素依次压至stack2,然后弹出栈顶元素
if(stack2.empty()){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
return stack2.pop();
//当stack2不为空时,直接弹出栈顶元素
}else{
return stack2.pop();
}
}
//拿到队列开头元素
public int peek() {
if(empty()){
return -1;
}
if(!stack2.empty()){
return stack2.peek();
}else{
while(!stack1.empty()){
stack2.push(stack1.pop());
}
return stack2.peek();
}
}
//判断队列是否为空
public boolean empty() {
return stack1.empty() && stack2.empty();
}
3.实现最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
push(x) —— 将元素 x 推入栈中。
pop() —— 删除栈顶的元素。
top() —— 获取栈顶元素。
getMin() —— 检索栈中的最小元素。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/min-stack
//定义两个栈
Stack<Integer> stack = new Stack<>(); //普通栈
Stack<Integer> minStack = new Stack<>(); //存放最小元素的栈
public MinStack() {
}
public void push(int x) {
//先在stack中压栈
stack.push(x);
//如果minStack为空,则把元素压入minStack
if(minStack.empty()){
minStack.push(x);
//minStack不为空时,将元素与minStack栈顶元素做比较
}else{
if(x <= minStack.peek()){
minStack.push(x);
}
}
}
//弹出栈顶元素
public void pop() {
int val = stack.pop();
if(val == minStack.peek()){
minStack.pop();
}
}
//获取栈顶元素
public int top() {
return stack.peek();
}
//检索栈中的最小元素
public int getMin() {
return minStack.peek();
}