当前位置:网站首页>剑指 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();
*/
边栏推荐
- Use indent to format code
- MHA High available Cluster for MySQL
- Count the number of inputs (C language)
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- 关于线性稳压器的五个设计细节
- Volatile of C language
- Zero length array in GNU C
- [trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
- C WinForm [help interface - send email] - practice five
- Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
猜你喜欢
Connection mode - bridge and net
Basic embedded concepts
More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
Installation and use of libjpeg and ligpng
实例010:给人看的时间
C WinForm [help interface - send email] - practice five
Keil use details -- magic wand
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Why is 1900 not a leap year
Carrier period, electrical speed, carrier period variation
随机推荐
Vofa+ software usage record
亿学学堂给的证券账户安不安全?哪里可以开户
Detailed explanation of SQL server stored procedures
C WinForm [exit application] - practice 3
Arduino uses nrf24l01+ communication
STM32 --- configuration of external interrupt
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Soem EtherCAT source code analysis I (data type definition)
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Hardware 1 -- relationship between gain and magnification
Brief discussion on Buck buck circuit
Simple design description of MIC circuit of ECM mobile phone
Reasons for rapid wear of conductive slip rings
Ble encryption details
General makefile (I) single C language compilation template
Management and use of DokuWiki
Tailq of linked list
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Summary -st2.0 Hall angle estimation
The firmware of the connected j-link does not support the following memory access