1. 栈和队列理论基础
栈不提供迭代器:
缺省
==“缺省”(默认)意味着如果没有显式指定某个选项或配置,则使用预定义的值或设置。==在上述提到的情况下,“以 deque 为缺省情况下栈的底层结构” 的意思是,如果在使用 SGI STL 时没有明确指定底层数据结构,则栈(stack)默认使用双端队列(deque)作为其底层实现。
例如,当我们使用 SGI STL 提供的 stack 容器时,如果我们没有通过特定的参数指定底层数据结构,则该 stack 容器会默认使用 deque 作为其底层实现。这意味着 stack 中的元素将按照双端队列的方式进行存储和操作。
2.用栈实现队列
用两个栈(栈先进后出,所以需要两个栈)模拟队列,队列是先进先出的
题意有说操作是有效的,不用去判断非法的情况
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
MyQueue() {
}
void push(int x) {
stIn.push(x);
}
//出队并返回该元素
int pop() {
if (stOut.empty()) {//只有出栈的栈为空,才把全部元素放到出栈的栈
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top(); //stOut不空 直接出栈
stOut.pop();
return result;
}
int peek() { //直接函数复用 因为在同个类里面 直接this 可以在同一个类中重复使用代码
int res = this->pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
3.用队列实现栈
class MyStack {
public:
queue<int> que1;
queue<int> que2;
MyStack() {
}
void push(int x) {
que1.push(x);
}
int pop() {
int size = que1.size();
size--;
while(size--) {
que2.push(que1.front());
que1.pop();
}
int result = que1.front();
que1.pop();
que1 = que2;
while (!que2.empty()) {
que2.pop();
}
return result;
}
int top() {
return que1.back();
}
bool empty() {
return que1.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
4.有效的括号
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();
}
return st.empty(); //遍历完了 就看栈为不为空了
}
};
5.删除字符串中的所有相邻重复项
我写的:
class Solution {
public:
string removeDuplicates(string s) {
stack <char> st;
for (int i = 0; i < s.size(); i++) {
if(st.empty()) {
st.push(s[i]);
} else if (st.top() == s[i]) {
st.pop();
} else if (st.top() != s[i]) {
st.push(s[i]);
}
}
string result = "";
while (!st.empty()) {
result += st.top();
st.pop();
}
reverse(result.begin(), result.end());
return result;
}
};
代码随想录的 先存在栈 再转为字符串:
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;
}
};
代码随想录的法二 拿字符串作为栈,省去了栈还要转为字符串的操作 :
class Solution {
public:
string removeDuplicates(string S) {
string result;
for(char s : S) {
if(result.empty() || result.back() != s) {
result.push_back(s);
}
else {
result.pop_back();
}
}
return result;
}
};
6.逆波兰表达式求值
class Solution {
public:
int evalRPN(vector<string>& tokens) {
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 {
st.push(stoll(tokens[i])); //stoll 是 C++ 中用于将字符串转换为长长整型(long long int)的函数。
}
}
int result = st.top();
st.pop();
return result;
}
};
7.滑动窗口最大值:
class Solution {
private:
class MyQueue {
public:
deque<int> que;
void pop(int value) {
if (!que.empty() && value == que.front()) { //value != que.front(),早被卷走了
que.pop_front();
}
}
void push(int value) {
while (!que.empty() && value > que.back()) { //全部出栈
que.pop_back();
}
que.push_back(value);
}
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++) {
que.push(nums[i]);
}
result.push_back(que.front());
for(int i = k; i < nums.size(); i++) {
que.pop(nums[i-k]);
que.push(nums[i]);
result.push_back(que.front());
}
return result;
}
};
8.前k个高频元素