当前位置:网站首页>剑指 Offer 09. 用两个栈实现队列
剑指 Offer 09. 用两个栈实现队列
2022-06-10 03:01:00 【C卷卷】
题目描述
思路

代码1
class CQueue {
public:
//加入队尾 appendTail() : 将数字 val 加入栈 A 即可。
// 删除队首deleteHead() : 有以下三种情况。
// 当栈 B 不为空: B中仍有已完成倒序的元素,因此直接返回 B 的栈顶元素。
// 否则,当 A 为空: 即两个栈都为空,无元素,因此返回 -1 。
// 否则: 将栈 A 元素全部转移至栈 B 中,实现元素倒序,并返回栈 B 的栈顶元素。
stack<int> A,B;
CQueue() {
}
void appendTail(int value) {
A.push(value);
}
int deleteHead() {
if(!B.empty()) {
int tmp = B.top();
B.pop();
return tmp;
}
if(A.empty()) {
return -1;
}
while(!A.empty()) {
int tmp = A.top();
A.pop();
B.push(tmp);
}
int tmp = B.top();
B.pop();
return tmp;
}
};
/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
感谢阅读
边栏推荐
- Knight Moves
- Esp32 intrinsic function / variable cannot jump to definition
- Some problems of scanf formatting input
- Xmake v2.6.6 release, Distributed Compilation and cache support
- 取消打印Tensorflow中的无用信息,如tensorflow:AutoGraph could not transform <*> and will run it as-is、加载CUDA信息等
- DataFrame日期数据处理
- Wechat applet music playing code (playing mode, lyrics scrolling)
- 2022.05 esp32 air upgrade OTA
- Compilation error of GStreamer source code in yocto image construction, causes and Solutions
- Xmake v2.6.6 release, distributed compilation and cache support
猜你喜欢

TS 23.122

ESP32 内部函数/变量无法跳转到定义

取消打印Tensorflow中的无用信息,如tensorflow:AutoGraph could not transform <*> and will run it as-is、加载CUDA信息等

TS 38.304

Technology dry goods | linkis practice: analysis of new engine implementation process

Arduino与Processing串口通信(match函数)

promise 介绍和实现

86.(leaflet之家)leaflet军事标绘-直线箭头采集

第六章 半导体存储器【微机原理】

leetcode:305. 岛屿的数量
随机推荐
Numpy matrix operation
^28js is single threaded
C#扩展方法(方法参数里的this)
17 orthogonal matrix and gram Schmidt orthogonalization
Educational Codeforces Round 129 (Rated for Div. 2)(A-D)
uwsgi loading shared libraries:libicui18n.so.58 异常处理
2022.05 esp32 air upgrade OTA
npm 报错 Class extends value undefined is not a constructor or null
bad interpreter:No such file or directory
Yocto实用技巧
在子集合中第一个元素是由另一个数组插入的
Analysis of the meaning of autocorrelation function / autocovariance function in different fields
promise 介绍和实现
22.对于BFS的思考
Xmake v2.6.6 发布,分布式编译和缓存支持
取消打印Tensorflow中的无用信息,如tensorflow:AutoGraph could not transform <*> and will run it as-is、加载CUDA信息等
bad interpreter:No such file or directory
The hcie Routing & switching test is postponed to December 31, 2022
1px problem
TS 38.304
