当前位置:网站首页>Sword finger offer 09 Implementing queues with two stacks
Sword finger offer 09 Implementing queues with two stacks
2022-06-12 00:35:00 【Clock_ zzd】
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]
Ideas :
A stack is responsible for putting elements into the queue , A stack is responsible for the element out of the queue .
class CQueue {
public:
stack<int> s1;
stack<int> s2;
CQueue() {
}
void appendTail(int value) {
s1.push(value);
}
int deleteHead() {
if (s1.empty() && s2.empty()) {
return -1;
} else if (s2.empty() && !s1.empty()) {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
int tmp = s2.top();
s2.pop();
return tmp;
}
};
边栏推荐
- 使用 select 切换协程
- Custom JSP tag - > concept - > lifecycle
- Shell (32): configure SSH privacy free
- How to package and send compressed files for mobile WPS
- ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
- Stm32f103c8t6 related knowledge
- The "hard words" about interface testing
- Cuiyunkai, CEO of Gewu titanium Intelligent Technology: data value jump, insight into the next generation of change forces
- WALLYS Qualcomm-Atheros-QCA9882-2T2R-MIMO-802.11ac-Mini-PCIe-WiFi 6E-Module-5G-high-power
- Flutter uses local pictures
猜你喜欢

oracle使用Navicat工具导入导出数据

自定义JSP标签->概念->生命周期

How much does it cost to develop s2b2c mall system

预解析与作用域

七个需要关注的测试自动化趋势

The time selector style is disordered and the middle text is blocked

How does Kingview use the wireless host link communication module to remotely collect PLC data?

SF14 | supertrend "super trend" indicator magic change and upgrade (source code)

2023 spring recruit | ant group middleware Intern Recruitment
![[industry application] an article on power digital transformation](/img/4a/d2d2795992cac8fb76135a117c3091.jpg)
[industry application] an article on power digital transformation
随机推荐
[day 5 of JUC learning] reference atomic classes and attribute modifiers
Flutter uses local pictures
Two configurations of data tables in efcore
2023 spring recruit | ant group middleware Intern Recruitment
At the digital data nextionbi online conference, traditional enterprises showed their in-depth understanding of data analysis
No permission to mount SMB share prompt directory
How to strengthen the prevention and control of major safety risks for chemical and dangerous goods enterprises in flood season
wps表格怎么取消智能表格样式
Dry goods | what do testers need to do for a complete performance test?
Exploration of qunar risk control safety products
How much does it cost to develop s2b2c mall system
挂载smb共享提示目录无权限
Adult education online training website open source
Pass the command parameters for operating mongodb to Mongo in the shell
Characteristics of JS logical operators
[day 6 of JUC learning] concurrent calculator and thread random factor
How to package and send compressed files for mobile WPS
Detailed explanation of merge sorting
Stm32f103c8t6 related knowledge
Understanding of black box testing by novices after years of testing