5.1 用栈实现队列
232.用栈实现队列
class MyQueue {
private:
// 双栈
stack<int> stack_in;
stack<int> stack_out;
public:
MyQueue() {}
void push(int x) {
stack_in.push(x);
}
int pop() {
if (stack_out.empty()) {
while (!stack_in.empty()) {
stack_out.push(stack_in.top());
stack_in.pop();
}
}
int result = stack_out.top();
stack_out.pop();
return result;
}
int peek() {
// 这里复用了pop函数,更加方便
int result = this->pop();
stack_out.push(result);
return result;
}
bool empty() {
return stack_in.empty() && stack_out.empty();
}
};
5.2 用队列实现栈
225.用队列实现栈
一个队列:
class MyStack {
private:
queue<int> que;
public:
MyStack() {
}
void push(int x) {
que.push(x);
// 这里一定要减一,否则最后进来的元素也换位置了
for (int i = 0; i < que.size() - 1; i++) {
que.push(que.front());
que.pop();
}
}
int pop() {
int result = que.front();
que.pop();
return result;
}
int top() {
return que.front();
}
bool empty() {
return que.empty();
}
};
两个队列:
class MyStack {
// 两个队列
private:
queue<int> que1;
queue<int> que2;
public:
MyStack() {}
void push(int x) {
que2.push(x); // 注意这里加到 que2 的
while (!que1.empty()) {
que2.push(que1.front());
que1.pop();
}
swap(que1, que2);
}
int pop() {
int result = que1.front();
que1.pop();
return result;
}
int top() {
return que1.front();
}
bool empty() {
return que1.empty();
}
};
5.3 有效的括号
20. 有效的括号
class Solution {
public:
bool isValid(string s) {
if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
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.top() 与 s[i]相等,栈弹出元素
}
// 第一种情况:此时我们已经遍历完了字符串,但是栈不为空(左括号多)
return st.empty();
}
};
5.4 删除字符串中所有的相邻重复项
1047.删除字符串中所有的相邻重复项
class Solution {
public:
string removeDuplicates(string Str) {
string result;
for (char s : Str) {
if (result.empty() || s != result.back())
result.push_back(s);
else
result.pop_back();
}
return result;
}
};
5.5 逆波兰表达式求值
150.逆波兰表达式求值
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int num1 = st.top();st.pop();
int num2 = st.top();st.pop();
if (tokens[i] == "+") st.push(num2 + num1); // 注意num2在前面
else if (tokens[i] == "-") st.push(num2 - num1);
else if (tokens[i] == "*") st.push(num2 * num1);
else st.push(num2 / num1);
} else {
st.push(stoi(tokens[i])); // 把string转换成int类型
}
}
int result = st.top();
st.pop();
return result;
}
};
5.6 滑动窗口最大值(单调队列)
239. 滑动窗口最大值
class Solution {
private:
class MyQue {
public:
deque<int> que;
void pop(int val) {
if (!que.empty() && val == que.front()) {
que.pop_front();
}
}
void push(int val) {
while (!que.empty() && val > que.back()) {
que.pop_back();
}
que.push_back(val);
}
int getMaxValue() {
return que.front();
}
};
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQue que;
vector<int> result;
for (int i = 0; i < k; i++) {
que.push(nums[i]);
}
result.push_back(que.getMaxValue());
for (int i = k; i < nums.size(); i++) {
que.pop(nums[i - k]);
que.push(nums[i]);
result.push_back(que.getMaxValue());
}
return result;
}
};
5.7 前k个高频元素(小顶堆)
347. 前k个高频元素
小顶堆
// 时间复杂度:O(nlogk)
// 空间复杂度:O(n)
class Solution {
public:
// 小顶堆
class mycomparison {
public:
bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second > rhs.second;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
// 要统计元素出现频率
unordered_map<int, int> map; // map<nums[i],对应出现的次数>
for (int i = 0; i < nums.size(); i++) {
map[nums[i]]++;
}
// 对频率排序
// 定义一个小顶堆,大小为k
priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;
// 用固定大小为k的小顶堆,扫面所有频率的数值
for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
pri_que.push(*it);
if (pri_que.size() > k) { // 如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
pri_que.pop();
}
}
// 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组
vector<int> result(k);
for (int i = k - 1; i >= 0; i--) {
result[i] = pri_que.top().first;
pri_que.pop();
}
return result;
}
};