代码随想录第十天| 232.用栈实现队列 |225. 用队列实现栈
  nvzbQoOT3qsa 2023年11月01日 111 0

因为之前比较忙期末考试=-= 所以断了打卡 现在 重新补起来~!

232.用栈实现队列 

题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/

看到题目的第一想法:因为一刷过,所以知道用两个栈来实现第一个栈用来存储

第二个栈就用来 将第一个栈的存储顺序变为队列的顺序

实现中遇到的困难:在实现pop的时候在想 如果转换到第二个栈之后如果还要添加怎么办

没有想到在删除之后 重新把第二个栈里面的元素放到第一个栈当中.

看到代码随想录之后的想法:进行还原 每一次要输出的时候再转化为队列(移到第二个栈当中)

class MyQueue
{
        stack<int> TheFirstStack;
        stack<int> TheSecoundStack;
public:
    MyQueue()
    {
    }
    void push(int x)
    {
        TheFirstStack.push(x);
    }

    int pop()
    {
        while(!TheFirstStack.empty())
        {
            auto tmp=TheFirstStack.top();
            TheFirstStack.pop();
            TheSecoundStack.push(tmp);
        }
        auto del=TheSecoundStack.top();
        TheSecoundStack.pop();
        while(!TheSecoundStack.empty())
        {
            auto tmp=TheSecoundStack.top();
            TheSecoundStack.pop();
            TheFirstStack.push(tmp);
        }
        return del;
    }

    int peek()
    {
        while(!TheFirstStack.empty())
        {
            auto tmp=TheFirstStack.top();
            TheFirstStack.pop();
            TheSecoundStack.push(tmp);
        }
        auto tmp=TheSecoundStack.top();
        while(!TheSecoundStack.empty())
        {
            auto tmp=TheSecoundStack.top();
            TheSecoundStack.pop();
            TheFirstStack.push(tmp);
        }
        return tmp;
    }

    bool empty()
    {
        if(TheFirstStack.empty())
        {
            return true;
        }
        return false;
    }
};

 

 

 

225. 用队列实现栈

题目链接:https://leetcode.cn/problems/implement-stack-using-queues/

看到题目的第一想法:因为一刷过.看到题目是要求使用两个队列,就想到用两个队列

实现中遇到的困难:因为做了第一个题陷入逻辑闭环,不知道怎么去实现

其实就是少循环队列大小一次,然后最后面的那个就是要移除的值.

看到代码随想录之后的想法:明白了如何实现

 1 class MyStack
 2 {
 3 public:
 4     queue<int> theFisrtQueue;
 5     queue<int> theSecoundQueue;
 6     MyStack()
 7     {
 8     }
 9 
10     void push(int x)
11     {
12         theFisrtQueue.push(x);
13     }
14 
15     int pop()
16     {
17         int size = theFisrtQueue.size();
18         size--;
19         while (size--)
20         {
21             auto tmp = theFisrtQueue.front();
22             theFisrtQueue.pop();
23             theSecoundQueue.push(tmp);
24         }
25         int result = theFisrtQueue.front();
26         theFisrtQueue.pop();
27         while (!theSecoundQueue.empty())
28         {
29             auto tmp = theSecoundQueue.front();
30             theSecoundQueue.pop();
31             theFisrtQueue.push(tmp);
32         }
33         return result;
34     }
35 
36     int top()
37     {
38         auto top=theFisrtQueue.back();
39         return top;
40 
41     }
42 
43     bool empty()
44     {
45         if (theFisrtQueue.empty())
46         {
47             return true;
48         }
49         return false;
50     }
51 };

 

用了不到一个小时,加油补上吧,虽然真的很累...

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jTMfQq5cr55P   2024年05月17日   42   0   0 算法与数据结构
  jTMfQq5cr55P   2024年05月17日   39   0   0 算法与数据结构
nvzbQoOT3qsa