• c++:堆和栈练习


    1.用队列实现栈

    请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

    优化:

    一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。

    1. #include
    2. #include
    3. using namespace std;
    4. class Mystack
    5. {
    6. public:
    7. queue<int> que;
    8. Mystack()
    9. {
    10. }
    11. void push(int x)
    12. {
    13. que.push();
    14. }
    15. int pop()
    16. {
    17. int size=que.size();
    18. size--;
    19. while(size--)
    20. {
    21. 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
    22. que.push(que.front());
    23. que.pop();
    24. }
    25. int result=que.front();
    26. que.pop();
    27. return result;
    28. }
    29. int top()
    30. {
    31. return que.back();
    32. }
    33. return empty()
    34. {
    35. return que.empty();
    36. }
    37. };

    分析:

    弹入:

     弹出

    2用栈实现队列

    请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

    实现 MyQueue 类:

    void push(int x) 将元素 x 推到队列的末尾
    int pop() 从队列的开头移除并返回元素
    int peek() 返回队列开头的元素
    boolean empty() 如果队列为空,返回 true ;否则,返回 false
    说明:

    你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

    1. class MyQueue {
    2. public:
    3. stack<int> stackin;
    4. stack<int> stackout;
    5. MyQueue() {
    6. }
    7. void push(int x) {
    8. stackin.push(x);
    9. }
    10. int pop() {
    11. if(stackout.empty())
    12. {
    13. while(!stackin.empty())
    14. {
    15. stackout.push(stackin.top());
    16. stackin.pop();
    17. }
    18. }
    19. int result=stackout.top();
    20. stackout.pop();
    21. return result;
    22. }
    23. int peek() {
    24. int res=this->pop();
    25. stackout.push(res);
    26. return res;
    27. }
    28. bool empty() {
    29. return stackin.empty()&&stackout.empty();
    30. }
    31. };
    32. /**
    33. * Your MyQueue object will be instantiated and called as such:
    34. * MyQueue* obj = new MyQueue();
    35. * obj->push(x);
    36. * int param_2 = obj->pop();
    37. * int param_3 = obj->peek();
    38. * bool param_4 = obj->empty();
    39. */

    分析

      

    3.有效括号

     

    给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。

    有效字符串需满足:

    左括号必须用相同类型的右括号闭合。
    左括号必须以正确的顺序闭合。
    每个右括号都有一个对应的相同类型的左括号。

    1. class Solution {
    2. public:
    3. bool isValid(string s) {
    4. if(s.size()%2!=0)return false;
    5. stack<char>st;
    6. for(int i=0;isize();i++)
    7. {
    8. if (s[i] == '(') st.push(')');
    9. else if (s[i] == '{') st.push('}');
    10. else if (s[i] == '[') st.push(']');
    11. else if(st.empty()||st.top()!=s[i])return false;
    12. else
    13. {
    14. st.pop();
    15. }
    16. }
    17. return st.empty();
    18. }
    19. };

    1047. 删除字符串中的所有相邻重复项

    栈的实现:

    1. class Solution {
    2. public:
    3. string removeDuplicates(string S) {
    4. stack<char> st;
    5. for (char s : S) {
    6. if (st.empty() || s != st.top()) {
    7. st.push(s);
    8. } else {
    9. st.pop(); // s 与 st.top()相等的情况
    10. }
    11. }
    12. string result = "";
    13. while (!st.empty()) { // 将栈中元素放到result字符串汇总
    14. result += st.top();
    15. st.pop();
    16. }
    17. reverse (result.begin(), result.end()); // 此时字符串需要反转一下
    18. return result;
    19. }
    20. };

     其实可以直接字符串

    1. class Solution {
    2. public:
    3. string removeDuplicates(string S) {
    4. string result;
    5. for(char s : S) {
    6. if(result.empty() || result.back() != s) {
    7. result.push_back(s);
    8. }
    9. else {
    10. result.pop_back();
    11. }
    12. }
    13. return result;
    14. }
    15. };

    150. 逆波兰表达式求值参考这篇文章

    1. class Solution {
    2. public:
    3. int evalRPN(vector& tokens) {
    4. stack<int> st;
    5. for (int i = 0; i < tokens.size(); i++) {
    6. if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
    7. int num1 = st.top();
    8. st.pop();
    9. int num2 = st.top();
    10. st.pop();
    11. if (tokens[i] == "+") st.push(num2 + num1);
    12. if (tokens[i] == "-") st.push(num2 - num1);
    13. if (tokens[i] == "*") st.push(num2 * num1);
    14. if (tokens[i] == "/") st.push(num2 / num1);
    15. } else {
    16. st.push(stoi(tokens[i]));
    17. }
    18. }
    19. int result = st.top();
    20. st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
    21. return result;
    22. }
    23. };

    代码bug:

    不能区分负号和减号

  • 相关阅读:
    python 数的计算
    3GPP协议解读(一)_23.501_23.502_PDU Session_SMF与UDP的交互
    2022年8月的10篇论文推荐
    同步`AAA`数据库下的`purse`表到`BBB`数据库下的同名表
    栈&队列OJ练习题(C语言版)
    系统安装技能测试
    KeyTool生成证书链及使用
    Presto RBO之 Sort算子优化
    基于Spring Boot的大学校园防疫与服务系统毕业设计源码111556
    深入Linux内核理解epoll事件轮询机制
  • 原文地址:https://blog.csdn.net/qq_62309585/article/details/126820758