• 【随想】每日两题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 使得添加的元素为首元素。以此类推所有的元素都是倒序排列的 也就是栈!

  • 相关阅读:
    报405和403错误
    04 光栅图形学算法-- 屏幕映射
    Python学习
    MYSQL之DQL(数据库查询语言)
    【每日一题】补档 ABC308E - MEX | 遍历保存 |简单
    Caché for UNIX®, Linux及macOS的安装及配置
    python-(4-5)数据类型的应用(字符集、编码和运算符)
    【牛客网题目详解】Q-前天是哪一天
    arthas诊断windows服务模式运行的Java进程
    CANopen协议 学习笔记
  • 原文地址:https://blog.csdn.net/weixin_63895720/article/details/134504740