当前位置:网站首页>Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
2022-06-25 23:51:00 【Program diary】
Brush questions these days , Every day I live by reading , Today I have another random question , Queues are implemented with two stacks , It feels good to read the title , But when I look at the input and output samples, I suddenly feel confused , I think this problem may be that the problem looks troublesome, but it's not troublesome to do , Here are the questions :
Topic link : Queues are implemented with two stacks
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 )
Example 1:
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]
Example 2:
Input :
[“CQueue”,“deleteHead”,“appendTail”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[],[5],[2],[],[]]
Output :[null,-1,null,null,5,2]
Tips :
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
The idea is also simple , Just two stacks ,a The stack is used to store the things in the team , When out of the team , hold a Everything in the stack pops up in turn , Push the b Stack , then b Stack again , Is to go out first , That is to say, FIFO is realized , Then, if you make a count , Join the team again or go to a In the stack , When b After leaving ( It's empty ), And then a Pop up and press in the stack in turn b Stack .
Generally speaking, it is ,a The stack is used to put the value of the queue ,b The stack is used to release the value of the team ,b When the stack is empty a The values in the stack are popped up and pushed in sequence b Just stack .
Here is the code , It is also a question to commemorate the following moments when I have ideas …
import java.util.Stack;
public class CQueue {
Stack<Integer> a;
Stack<Integer> b;
public CQueue() {
// Find two stacks , A stack of data , Another stack makes a queue operation
a = new Stack<>();
b = new Stack<>();
}
public void appendTail(int value) {
// Joining operation
a.push(value);
}
public int deleteHead() {
if (b.empty()){
int size = a.size();
// b The stack is empty. , Then put a Put things inside to operate
for (int i = 0; i < size; i++){
b.push(a.pop());
}
}
try{
Integer pop = b.pop();
if (pop != null) return pop;
}catch (Exception e){
return -1;
}
return -1;
}
}
边栏推荐
- Hibernate core api/ configuration file / L1 cache details
- The package name of the manifest file in the library project and the app project are not the same
- DPVS-FullNAT模式部署篇
- String object (constant) pool
- 推荐系统设计
- Doris 运维中遇到的问题
- Can I upload pictures without deploying the server?
- QT Chinese and English use different fonts respectively
- 213.打家劫舍 II
- MySQL version upgrade + data migration
猜你喜欢
随机推荐
达梦数据库修改字段信息采坑记
debezium
How to generate get/set methods in idea
Gradle的环境安装与配置
SSL/TLS、对称加密和非对称加密和TLSv1.3
数据同步
7.常用指令(下)v-on,v-bind,v-model的常见操作
line-height小用
树莓派开机发送热点进行远程登录
Hibernate core api/ configuration file / L1 cache details
unsigned与signed之大白话
流数据
解析產品開發失敗的5個根本原因
Database - mongodb
213.打家劫舍 II
给定参数n,从1到n会有n个整数1,2,3,...,n,这n个数组共有n!种排列,按照大小顺序升序排列出所有列的情况,并一一标记,给定n和k,返回第k个值
MySQL InnoDB lock knowledge points
Doris 运维中遇到的问题
About the swoole coroutine container
Understanding of pseudo classes









