当前位置:网站首页>Sword finger offer 𞓜: stack and queue (simple)
Sword finger offer 𞓜: stack and queue (simple)
2022-06-27 02:13:00 【_ Soren】
List of articles
- 【 Stack and queue 】
List of articles
Preface
This column records leetcode Record of writing questions , With a sword finger Offer The first 2 Version based
1. Simulate the queue with two stacks
Original link : The finger of the sword Offer 09. Queues are implemented with two stacks
subject :
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 :
Input :
[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”]
[[],[3],[],[]]
Output :[null,null,3,-1]
1 <= values <= 10000
At most appendTail、deleteHead Conduct 10000 Secondary call
Code
class CQueue {
private:
stack<int> s1;
stack<int> s2;
void push_In_S2() {
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
public:
CQueue() {
}
// Queue in operation , Push s1
void appendTail(int value) {
s1.push(value);
}
// The stack,
int deleteHead() {
if (s2.empty()) {
if (s1.empty()) {
return -1;
}
push_In_S2(); // This function is responsible for s1 Element of is pushed to s2
}
int value = s2.top();
s2.pop();
return value;
}
};
/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
2. contain min Function of the stack
subject :
Defines the data structure of the stack , Please implement a in this type that can get the minimum elements of the stack min The function is in the stack , call min、push And pop The time complexity of O(1).
Original link : contain min Function of the stack
Code :
With the help of auxiliary stack , Record the minimum value every time you stack , Keep the minimum at the top of the auxiliary stack .
class MinStack {
private:
stack<int> stk;
stack<int> min_stk;
public:
/** initialize your data structure here. */
MinStack() {
min_stk.push(INT_MAX);
}
void push(int x) {
stk.push(x);
min_stk.push(std::min(min_stk.top(), x));
}
void pop() {
stk.pop();
min_stk.pop();
}
int top() {
return stk.top();
}
int min() {
return min_stk.top();
}
};
边栏推荐
- 平均风向风速计算(单位矢量法)
- Oracle/PLSQL: CharToRowid Function
- Is the division of each capability domain of Dama, dcmm and other data management frameworks reasonable? Is there internal logic?
- lottie.js创意开关按钮动物头像
- YaLM 100B:来自俄罗斯Yandex的1000亿参数开源大模型,允许商业用途
- Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
- Consumers pursue the iPhone because its cost performance exceeds that of domestic mobile phones
- Oracle/PLSQL: Trim Function
- Oracle/PLSQL: Length Function
- memcached基础10
猜你喜欢
随机推荐
Don't be brainwashed. This is the truth about the wages of 90% of Chinese people
Microsoft365 developer request
Oracle/PLSQL: To_ Clob Function
达梦数据库的卸载
Flink学习3:数据处理模式(流批处理)
Arbre binaire OJ sujet
参数估计——《概率论及其数理统计》第七章学习报告(点估计)
Oracle/PLSQL: Ltrim Function
mmdetection ValueError: need at least one array to concatenate解决方案
Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
Flink learning 3: data processing mode (stream batch)
Flink学习5:工作原理
二叉樹oj題目
Oracle/PLSQL: HexToRaw Function
Oracle/PLSQL: From_Tz function
Memcached foundations 12
Oracle/PLSQL: Lower Function
ThreadLocal详解
SQLite Reader 插件测试SQLite语法
Oracle/PLSQL: Translate Function








