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 res=stOut.top();
stOut.pop();
// if(stIn.empty()){
// while(!stOut.empty()){
// stIn.push(stOut.top());
// stOut.pop();
// }
// }
return res;
}
int peek() {
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();
*/
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 res=que1.front();
que1.pop();
while(!que2.empty()){
que1.push(que2.front());
que2.pop();
}
return res;
}
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();
*/
class Solution {
public:
bool isValid(string s) {
if(s.size()%2!=0) return false;
stack<int>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()||s[i]!=st.top()) return false;
else st.pop();
}
return st.empty();
}
};
class Solution {
public:
string removeDuplicates(string s) {
stack<int>st;
for(int i=0;i<s.size();i++){
if(st.empty()||s[i]!=st.top()) st.push(s[i]);
else st.pop();
}
string res="";
while(!st.empty()){
res+=st.top();
st.pop();
}
reverse(res.begin(),res.end());
return res;
}
};
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 {
st.push(stoll(tokens[i]));
}
}
int result = st.top();
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
return result;
}
};
单调队列
class Solution {
public:
class MyQueue{
public:
deque<int>que;
void pop(int val){
if(!que.empty()&&que.front()==val){
que.pop_front();
}
}
void push(int val){
while(!que.empty()&&que.back()<val){
que.pop_back();
}
que.push_back(val);
}
int front(){
return que.front();
}
};
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
MyQueue que;
vector<int>res;
for(int i=0;i<k;i++){
que.push(nums[i]);
}
res.push_back(que.front());
for(int i=k;i<nums.size();i++){
que.pop(nums[i-k]);
que.push(nums[i]);
res.push_back(que.front());
}
return res;
}
};
优选队列
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) {
//加入map,统计频率
unordered_map<int,int>map;
for(int i=0;i<nums.size();i++){
map[nums[i]]++;
}
//创建优先队列
priority_queue<pair<int,int>,vector<pair<int,int>>,mycomparison> pri_que;
//用队列对map中统计的进行排序
for(auto it=map.begin();it!=map.end();it++){
pri_que.push(*it);
if(pri_que.size()>k) {
pri_que.pop();
}
}
//保存队列中的k个元素
vector<int>res(k);
for(int i=0;i<k;i++){
res[i]=pri_que.top().first;
pri_que.pop();
}
return res;
}
};