232. 用栈实现队列
leetcode题目链接
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
MyQueue() {
}
// 送元素入队列
void push(int x) {
stIn.push(x);
}
// 出队列
int pop() {
//当stOut是空 到时候 从stIn中导入数据 导入stIng中全部的数据
if(stOut.empty()){
while(!stIn.empty()){
stOut.push(stIn.top());//用top获取stIn栈顶元素 然后放入StOut中
stIn.pop();//stIn丢掉该元素
}
}
int result = stOut.top();
stOut.pop();
return result;
}
int peek() {
int res = this->pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
225. 用队列实现栈
leletcode题目链接添加链接描述
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int size = que.size();
size--;
while(size--){
que.push(que.front());// 将队列顶部的元素送到入口处
que.pop();//将该元素送出去
}
int result = que.front();
que.pop();
return result;
}
int top() {
return que.back();
}
bool empty() {
return que.empty();
}
};
20. 有效的括号
leetcode题目链接
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
class Solution {
public:
bool isValid(string s) {
if(s.size() % 2 != 0) return false;
stack<char> st;
for(int i = 0; i < s.size(); i++){
if(s[i] =='(') st.push(')');
else if(s[i] =='[') st.push(']');
else if(s[i] =='{') st.push('}');
else if(st.empty() || st.top() != s[i]) return false;
else st.pop();
}
//st.empty() 是用来检查栈 st 是否为空的方法 如果为空 则返回true
return st.empty();//遍历完了之后 如果栈不为空 就返回false
}
};
1047. 删除字符串中的所有相邻重复项
leetcode题目链接
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
class Solution {
public:
string removeDuplicates(string S ) {
stack<char> st;
for(char s : S ){
//如果当前元素与栈顶元素不相等 就将该元素送入栈中
if(st.empty()||s!=st.top()){
st.push(s);
}
else{
st.pop();//如果相等 则需要删除 将该元素吐出去
}
}
string result = "";
while(!st.empty()){
result += st.top();
st.pop();
}
reverse(result.begin(),result.end());
return result;
}
};
- 逆波兰表达式求值
leetcode题目链接
class Solution {
public:
int evalRPN(vector<string>& tokens) {
// 力扣修改了后台测试数据,需要用longlong
stack<long long> st;
for (int i = 0; i < tokens.size(); i++) {
//如果遇见操作符就进行运算
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
long long num1 = st.top();
st.pop();
long long num2 = st.top();
st.pop();
if (tokens[i] == "+") st.push(num2 + num1);
if (tokens[i] == "-") st.push(num2 - num1);
if (tokens[i] == "*") st.push(num2 * num1);
if (tokens[i] == "/") st.push(num2 / num1);
}
//如果没有遇见操作符 就推元素进栈
else {
//stoll:将字符串转换为长长整型(long long)的函数
st.push(stoll(tokens[i]));
}
}
int result = st.top();
st.pop();
return result;
}
};
239. 滑动窗口最大值
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回 滑动窗口中的最大值 。
leetcode题目链接
class Solution {
public:
class MyQueue{
public:
// 实现单调队列
deque<int> que;// 双端队列(double-ended queue)的数据结构 支持在两端进行高效的插入和删除操作
//定义pop 弹出时候要判断弹出的数值是否等于队列出口的元素 如果相等则弹出
void pop(int value){
if(!que.empty()&& value == que.front()){
que.pop_front();
}
}
//定义push 如果push的数值大于入口元素的数值 就将队列后端的数值弹出 直到push的数值小于等于队列入口元素的数值为止
// 这样就保证了队列的数值是单调从大到小的了
void push(int value) {
while (!que.empty() && value > que.back()) {
que.pop_back();
}
que.push_back(value);
}
//定义最大值返回 查询当前队列的最大值 直接返回队列前端也就是front就可以了
int front(){
return que.front();
}
};
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQueue que;
vector<int> result;
for (int i = 0; i < k; i++) { // 先将前k的元素放进队列
que.push(nums[i]);
}
result.push_back(que.front()); // result 记录前k的元素的最大值
for (int i = k; i < nums.size(); i++) {
que.pop(nums[i - k]); // 滑动窗口移除最前面元素
que.push(nums[i]); // 滑动窗口前加入最后面的元素
result.push_back(que.front()); // 记录对应的最大值
}
return result;
}
};