225. 用队列实现栈
class MyStack {public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {q.push(x);for(int i=0;i<q.size()-1;i++){q.push(q.front());q.pop();}}/** Removes the element on top of the stack and returns that element. */int pop() {int x = q.front();q.pop();return x;}/** Get the top element. */int top() {return q.front();}/** Returns whether the stack is empty. */bool empty() {return q.empty();}private:std::queue<int> q;};/*** 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();*/
232. 用栈实现队列
class MyQueue {public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {while(!s.empty()) {temp.push(s.top());s.pop();}temp.push(x);while(!temp.empty()){s.push(temp.top());temp.pop();}}/** Removes the element from in front of queue and returns that element. */int pop() {int x = s.top();s.pop();return x;}/** Get the front element. */int peek() {return s.top();}/** Returns whether the queue is empty. */bool empty() {return s.empty();}private:std::stack<int> s,temp;};/*** 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();*/
155. 最小栈
使用另外一个栈保存最小值
#include <limits.h> // INT_MAX =2147483647 的头文件class MinStack {public:/** initialize your data structure here. */MinStack() {mins.push(INT_MAX);}void push(int x) {s.push(x);mins.push(min(mins.top(),x));}void pop() {s.pop();mins.pop();}int top() {return s.top();}int getMin() {return mins.top();}private:std::stack<int> s,mins;};/*** Your MinStack object will be instantiated and called as such:* MinStack* obj = new MinStack();* obj->push(x);* obj->pop();* int param_3 = obj->top();* int param_4 = obj->getMin();*/
合法的出栈序列
已知从1到n的数字序列,按顺序入栈,每个数字入栈后即可出栈,也可在栈中停留,等待后面的数字入栈出栈后,该数字再出栈,求该数字序列的出栈序列是否合法?
//// Created by er on 2020/5/26.//判断正常序列// 使用栈和队列模拟过程//#include <iostream>#include <queue>#include <stack>using namespace std;bool check_is_valid_order(queue<int> &order){stack<int > s;int n=order.size();for(int i=i;i<=n;i++){s.push(i);while(!s.empty()&&s.top()== order.front()){s.pop();order.pop();}}if(!s.empty()) return false;else return true;}int main(){return 0;}
224. 基本计算器
实现一个基本的计算器来计算一个简单的字符串表达式的值
//
// Created by er on 2020/6/4.
//
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class solution{
public:
int calculate(string s){
stack<int> st;
int sign = 1;
int res =0;// 保存结果
for(int i=0;i<s.size();i++){
//数字
int num =0;//记录中间变量
if(s[i] >= '0'&&s[i] <= '9'){
while(s[i]>='0'&&s[i]<='9'&&i < s.size()){
num = num * 10 +(s[i]-'0');
i++;
}
i--;
res = res + sign * num;
} else if(s[i]== '+'){
sign =1;
} else if(s[i] == '-'){
sign = -1;
} else if(s[i]== '('){ // 将当前的结果先保存到栈中,并且置res 为0,计算括号内的值
st.push(res);
st.push(sign);
res = 0;
sign =1;// 恢复初始状态
} else if(s[i] == ')'){
res *= st.top();st.pop();
res+= st.top(); st.pop();
} else cout << "Eroor Input!" << endl;
}
return res;
}
};
int main(){
string s= "3+5-(2+3)";
string s1 = "-3+5+3";
string s2 = "((-33+5)-(5+2)-1)";
solution cal;
int res = cal.calculate(s2);
cout << res << endl;
return 0;
}
215. 数组中的第K个最大元素
使用最小堆优先级队列实现
//
// Created by er on 2020/6/4.
//
// 优先级队列最小堆实现无序数组中第k大的数
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class solution{
public:
int KstNumArray(vector<int> &v,int k){
priority_queue<int,vector<int>,greater<int>> pq;
for(int i=0;i<k;i++){
pq.push(v[i]);
}
for(int i=k;i<v.size();i++){
if(v[i]>pq.top()){
pq.pop();
pq.push(v[i]);
}
}
return pq.top();
}
};
int main(){
vector<int > v={1,2,5,3,55,46,66};
solution s;
int res = s.KstNumArray(v,4);
cout << res << endl;
return 0;
}
