当前位置:网站首页>剑指 Offer 09. 用两个栈实现队列
剑指 Offer 09. 用两个栈实现队列
2022-07-05 08:16:00 【程序员·小李】
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )
示例 1:
输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]
示例 2:
输入:
["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
[[],[],[5],[2],[],[]]
输出:[null,-1,null,null,5,2]
提示:
1 <= values <= 10000
最多会对 appendTail、deleteHead 进行 10000 次调用
思路:
1. 一个插入栈,一个输出栈。插入数据时,往插入栈中写。
2. 输出数据时,优先从输出栈弹出,如果输出栈中不存在,则依次从输入栈中压入。
class CQueue {
Stack<Integer> pushStack = new Stack<Integer>();
Stack<Integer> popStack = new Stack<Integer>();
public CQueue() {
}
public void appendTail(int value) {
pushStack.push(value);
}
public int deleteHead() {
if (popStack.empty()){
while (!pushStack.empty()){
popStack.push(pushStack.pop());
}
}
if (popStack.empty()){
return -1;
}
return popStack.pop();
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/
边栏推荐
- STM32 virtualization environment of QEMU
- Simple design description of MIC circuit of ECM mobile phone
- Network communication process
- Various types of questions judged by prime numbers within 100 (C language)
- UE像素流,来颗“减肥药”吧!
- Briefly talk about the identification protocol of mobile port -bc1.2
- Summary of SIM card circuit knowledge
- Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
- [tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
- Network port usage
猜你喜欢
Solutions to compilation warnings in Quartus II
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
STM32 single chip microcomputer - bit band operation
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Halcon's practice based on shape template matching [1]
Circleq of linked list
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
Network communication process
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Carrier period, electrical speed, carrier period variation
随机推荐
Embedded composition and route
Consul installation
NTC thermistor application - temperature measurement
C WinForm [realize the previous and next selection pictures] - practice 7
Soem EtherCAT source code analysis I (data type definition)
C WinForm [help interface - send email] - practice five
Sizeof (function name) =?
Shell script basic syntax
OC and OD gate circuit
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
STM32 single chip microcomputer - bit band operation
Talk about the function of magnetic beads in circuits
List of linked lists
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Basic information commands and functions of kernel development
MySQL MHA high availability cluster
DokuWiki deployment notes
STM32---IIC
Take you to understand the working principle of lithium battery protection board
[paper reading] the latest transfer ability in deep learning: a survey in 2022