• 【随想】每日两题Day.17


    题目232. 用栈实现队列

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

    实现 MyQueue 类:

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

    说明:

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

    代码:

    1. class MyQueue {
    2. Stack stack1; //必须在类中声明成员变量
    3. Stack stack2; //然后在构造方法中 初始化!!!
    4. public MyQueue() {
    5. stack1 = new Stack<>();
    6. stack2 = new Stack<>();
    7. }
    8. public void push(int x) {
    9. stack1.push(x);
    10. }
    11. public int pop() {
    12. if(!stack2.empty()) {
    13. return stack2.pop();
    14. }
    15. while(!stack1.empty()) {
    16. stack2.push(stack1.pop());
    17. }
    18. return stack2.pop();
    19. }
    20. public int peek() {
    21. if(!stack2.empty()) {
    22. return stack2.peek();
    23. }
    24. while(!stack1.empty()) {
    25. stack2.push(stack1.pop());
    26. }
    27. return stack2.peek();
    28. }
    29. public boolean empty() {
    30. return (stack1.empty() && stack2.empty());
    31. }
    32. }

    思考:

    此题我犯了一个天大的错误,竟然在构造方法里声明stack1和stack2。在方法中的局部变量怎么能给其他方法提供使用呢!!! 应该在类中声明成员变量,然后在构造方法初始化!构造方法不就是用来初始化的吗!

    题目225. 用队列实现栈

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

    实现 MyStack 类:

    • void push(int x) 将元素 x 压入栈顶。
    • int pop() 移除并返回栈顶元素。
    • int top() 返回栈顶元素。
    • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

    注意:

    • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsize 和 is empty 这些操作。
    • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

    代码一:使用两个队列实现栈

    1. class MyStack {
    2. Queue queue1;
    3. Queue queue2;
    4. public MyStack() {
    5. queue1 = new LinkedList<>();
    6. queue2 = new LinkedList<>();
    7. }
    8. public void push(int x) {
    9. if(queue1.isEmpty()){
    10. queue2.add(x);
    11. } else {
    12. queue1.add(x);
    13. }
    14. }
    15. public int pop() {
    16. int res = 0;
    17. if(queue1.isEmpty()){
    18. while(!queue2.isEmpty()) {
    19. res = queue2.remove();
    20. if(!queue2.isEmpty()) queue1.add(res);
    21. }
    22. } else {
    23. while(!queue1.isEmpty()) {
    24. res = queue1.remove();
    25. if(!queue1.isEmpty()) queue2.add(res);
    26. }
    27. }
    28. return res;
    29. }
    30. public int top() {
    31. int res = 0;
    32. if(queue1.isEmpty()){
    33. while(!queue2.isEmpty()) {
    34. res = queue2.remove();
    35. queue1.add(res);
    36. }
    37. } else {
    38. while(!queue1.isEmpty()) {
    39. res = queue1.remove();
    40. queue2.add(res);
    41. }
    42. }
    43. return res;
    44. }
    45. public boolean empty() {
    46. return (queue1.isEmpty() && queue2.isEmpty());
    47. }
    48. }

    思考一:

    这个思路不难,问题的核心就是如何找到最后一个元素。就是两个队列哪个空就把元素都放到空的那个 然后知道走到最后一个元素。

    代码二:使用一个队列实现栈

    1. class MyStack {
    2. Queue queue;
    3. public MyStack() {
    4. queue = new LinkedList<>();
    5. }
    6. public void push(int x) {
    7. queue.add(x);
    8. int size = queue.size();
    9. while(size != 1) {
    10. queue.add(queue.remove());
    11. size--;
    12. }
    13. }
    14. public int pop() {
    15. return queue.remove();
    16. }
    17. public int top() {
    18. return queue.peek();
    19. }
    20. public boolean empty() {
    21. return queue.isEmpty();
    22. }
    23. }

    思考二:

    使用一个队列实现栈,就是每次push时将队列中所有元素,都remove再add 使得添加的元素为首元素。以此类推所有的元素都是倒序排列的 也就是栈!

  • 相关阅读:
    【Linux】环境变量
    数据挖掘(四)KNN
    什么是多云? 为什么我们需要多云可观测性 (Observability)?
    【Mysql】mysql学习之旅01-相关概念
    GBase产品学习-SetSysEnv.py脚本
    MySQL:常用函数解析、开窗函数示例
    每个数据工程师都应该了解和使用的10 个 ChatGPT 提示
    正则表达式(Perl 示例)
    【Github】将本地仓库同步到github上
    华为数字化转型之道 方法篇 第四章 用变革的方法确保规划落地
  • 原文地址:https://blog.csdn.net/weixin_63895720/article/details/134504740