当前位置:网站首页>剑指 Offer 09. 用两个栈实现队列
剑指 Offer 09. 用两个栈实现队列
2022-06-25 15:32:00 【anieoo】
solution:
class CQueue {
public:
stack<int> stk_i;
stack<int> stk_o;
CQueue() {
}
void appendTail(int value) {
stk_i.push(value);
}
int deleteHead() {
if(stk_i.empty() && stk_o.empty()) return -1; //两个栈都为空,返回-1
if(!stk_o.empty()) { //出队栈不为空,直接返回
int res = stk_o.top();
stk_o.pop();
return res;
}
else {
while(!stk_i.empty()) {
stk_o.push(stk_i.top());
stk_i.pop();
}
int res = stk_o.top();
stk_o.pop();
return res;
}
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
边栏推荐
- 网上办理股票开户安全吗?
- 剑指 Offer 03. 数组中重复的数字
- 在国信金太阳开股票账户安全吗?
- Advertising effect cluster analysis (kmeans)
- Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
- The situation and suggestions of a software engineering associate graduate who failed in the postgraduate entrance examination
- 5 connection modes of QT signal slot
- JS capture, target, bubble phase
- Basic syntax and common commands of R language
- Stack and queue
猜你喜欢

Several common optimization methods

CV pre training model set

1090.Phone List

Architecture evolution of high-performance servers -- Suggestions

How to download and install Weka package

Single user mode
![[paper notes] rethinking and improving relative position encoding for vision transformer](/img/6b/8b1c192e0ce715789465fa9ccaabfc.jpg)
[paper notes] rethinking and improving relative position encoding for vision transformer

Summary of four parameter adjustment methods for machine learning

Agent and classloader

Postman usage notes, interface framework notes
随机推荐
JS select all exercise
Internal class learning notes
Is it safe to open a stock account online?
剑指 Offer 06. 从尾到头打印链表
Bessie's weight problem [01 backpack]
QT pattern prompt box implementation
Some usage records about using pyqt5
双目3D感知(一):双目初步认识
Design and implementation of thread pool
Why do I need message idempotence?
[paper notes] semi supervised object detection (ssod)
Why should the coroutine be set to non blocking IO
Using R language in jupyter notebook
国信金太阳靠谱吗?是否合法?开股票账户安全吗?
Solution of push code failure in idea
Esp8266 building smart home system
CV pre training model set
Cross compilation correlation of curl Library
0703 interface automation - MySQL database connection, encapsulation, adding database verification in use cases
Shared memory synchronous encapsulation