232.用栈实现队列
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.pop();
return result;
}
int peek() {
int res = this->pop();
stOut.push(res);
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();
}
};
225. 用队列实现栈
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int size = que.size()-1;
while(size--)
{
que.push(que.front());
que.pop();
}
int result = que.front();
que.pop();
return result;
}
int top() {
int size = que.size()-1;
while(size--)
{
que.push(que.front());
que.pop();
}
int result = que.front();
que.push(que.front());
que.pop();
return result;
}
bool empty() {
return que.empty();
}
};
20. 有效的括号
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i=0;i<s.length();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();
}
};
1047. 删除字符串中的所有相邻重复项
class Solution {
public:
string removeDuplicates(string S) {
stack<char> st;
for(char ch : S)
{
if(st.empty() || ch != st.top()) // 先检查堆栈是否为空
{
st.push(ch);
}
else{
st.pop();
}
}
string ss;
while(!st.empty())
{
ss += st.top();
st.pop();
}
reverse(ss.begin(),ss.end());
return ss;
}
};
150. 逆波兰表达式求值
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]));
}
}
return st.top();
}
};
239. 滑动窗口最大值
class Solution {
public:
class Myqueue
{
public:
deque<int> que;
void pop(int x)
{
if(!que.empty() && x == que.front())
{
que.pop_front();
}
}
void push(int val)
{
while(!que.empty() && val>que.back())
{
que.pop_back();
}
que.push_back(val);
}
int front()
{
return que.front();
}
};
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;
}
};
347.前 K 个高频元素
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> result;
unordered_map<int,int> umap;
for(int num : nums)
{
umap[num]++;
}
auto comp = [](pair<int,int> &a,pair<int,int> &b)
{
return a.second > b.second;
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(comp)> minHeap(comp);
for (auto& [num, freq] : umap) {
minHeap.push({num, freq});
if (minHeap.size() > k) {
minHeap.pop();
}
}
while (!minHeap.empty()) {
result.push_back(minHeap.top().first);
minHeap.pop();
}
return result;
}
};