当前位置:网站首页>剑指 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();
*/
边栏推荐
- Compilation warning solution sorting in Quartus II
- Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
- STM32 tutorial triple ADC interleaved sampling
- 【论文阅读】2022年最新迁移学习综述笔注(Transferability in Deep Learning: A Survey)
- Classic application of MOS transistor circuit design (2) - switch circuit design
- Embedded composition and route
- MySQL之MHA高可用集群
- Several important parameters of LDO circuit design and type selection
- go依赖注入--google开源库wire
- Makefile application
猜你喜欢

STM32 virtualization environment of QEMU
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6

Explain task scheduling based on Cortex-M3 in detail (Part 2)

How to select conductive slip ring

Network port usage

Semiconductor devices (I) PN junction

Arduino uses nrf24l01+ communication

Nb-iot technical summary

Explain task scheduling based on Cortex-M3 in detail (Part 1)

99 multiplication table (C language)
随机推荐
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Anonymous structure in C language
Process communication mode between different hosts -- socket
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Embedded composition and route
FIO测试硬盘性能参数和实例详细总结(附源码)
DCDC circuit - function of bootstrap capacitor
Wifi-802.11 negotiation rate table
Stm32--- systick timer
matlab timeserise
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
C WinForm [view status bar -- statusstrip] - Practice 2
PMSM dead time compensation
实例009:暂停一秒输出
Consul installation
Vofa+ software usage record
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
STM32 --- GPIO configuration & GPIO related library functions
Shell script
Basic embedded concepts