225. 用队列实现栈

  1. class MyStack {
  2. public:
  3. /** Initialize your data structure here. */
  4. MyStack() {
  5. }
  6. /** Push element x onto stack. */
  7. void push(int x) {
  8. q.push(x);
  9. for(int i=0;i<q.size()-1;i++){
  10. q.push(q.front());
  11. q.pop();
  12. }
  13. }
  14. /** Removes the element on top of the stack and returns that element. */
  15. int pop() {
  16. int x = q.front();
  17. q.pop();
  18. return x;
  19. }
  20. /** Get the top element. */
  21. int top() {
  22. return q.front();
  23. }
  24. /** Returns whether the stack is empty. */
  25. bool empty() {
  26. return q.empty();
  27. }
  28. private:
  29. std::queue<int> q;
  30. };
  31. /**
  32. * Your MyStack object will be instantiated and called as such:
  33. * MyStack* obj = new MyStack();
  34. * obj->push(x);
  35. * int param_2 = obj->pop();
  36. * int param_3 = obj->top();
  37. * bool param_4 = obj->empty();
  38. */

232. 用栈实现队列

  1. class MyQueue {
  2. public:
  3. /** Initialize your data structure here. */
  4. MyQueue() {
  5. }
  6. /** Push element x to the back of queue. */
  7. void push(int x) {
  8. while(!s.empty()) {
  9. temp.push(s.top());
  10. s.pop();
  11. }
  12. temp.push(x);
  13. while(!temp.empty()){
  14. s.push(temp.top());
  15. temp.pop();
  16. }
  17. }
  18. /** Removes the element from in front of queue and returns that element. */
  19. int pop() {
  20. int x = s.top();
  21. s.pop();
  22. return x;
  23. }
  24. /** Get the front element. */
  25. int peek() {
  26. return s.top();
  27. }
  28. /** Returns whether the queue is empty. */
  29. bool empty() {
  30. return s.empty();
  31. }
  32. private:
  33. std::stack<int> s,temp;
  34. };
  35. /**
  36. * Your MyQueue object will be instantiated and called as such:
  37. * MyQueue* obj = new MyQueue();
  38. * obj->push(x);
  39. * int param_2 = obj->pop();
  40. * int param_3 = obj->peek();
  41. * bool param_4 = obj->empty();
  42. */

155. 最小栈

使用另外一个栈保存最小值

  1. #include <limits.h> // INT_MAX =2147483647 的头文件
  2. class MinStack {
  3. public:
  4. /** initialize your data structure here. */
  5. MinStack() {
  6. mins.push(INT_MAX);
  7. }
  8. void push(int x) {
  9. s.push(x);
  10. mins.push(min(mins.top(),x));
  11. }
  12. void pop() {
  13. s.pop();
  14. mins.pop();
  15. }
  16. int top() {
  17. return s.top();
  18. }
  19. int getMin() {
  20. return mins.top();
  21. }
  22. private:
  23. std::stack<int> s,mins;
  24. };
  25. /**
  26. * Your MinStack object will be instantiated and called as such:
  27. * MinStack* obj = new MinStack();
  28. * obj->push(x);
  29. * obj->pop();
  30. * int param_3 = obj->top();
  31. * int param_4 = obj->getMin();
  32. */

合法的出栈序列

已知从1到n的数字序列,按顺序入栈,每个数字入栈后即可出栈,也可在栈中停留,等待后面的数字入栈出栈后,该数字再出栈,求该数字序列的出栈序列是否合法?

  1. //
  2. // Created by er on 2020/5/26.
  3. //判断正常序列
  4. // 使用栈和队列模拟过程
  5. //
  6. #include <iostream>
  7. #include <queue>
  8. #include <stack>
  9. using namespace std;
  10. bool check_is_valid_order(queue<int> &order){
  11. stack<int > s;
  12. int n=order.size();
  13. for(int i=i;i<=n;i++){
  14. s.push(i);
  15. while(!s.empty()&&s.top()== order.front()){
  16. s.pop();
  17. order.pop();
  18. }
  19. }
  20. if(!s.empty()) return false;
  21. else return true;
  22. }
  23. int main(){
  24. return 0;
  25. }

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;
}