当前位置:   article > 正文

代码随想录算法训练营(JAVA) | 第五章 栈与队列part01

代码随想录算法训练营(JAVA) | 第五章 栈与队列part01

      今日任务 

力扣 232. 用栈实现队列,   225. 用队列实现栈

无论是栈实现队列还是队列实现栈都是用两个

而队列由于其特性能达到只使用一个实现,由于其还有Deque接口更是能做到只是用单个解决问题

题目 :232. 用栈实现队列

思路

先搞清楚我要什么  FIFO 实现 FILO

题解

需要注意的是:当push和pop前先要去考虑 辅助的stack中是否还有值?怎么处理?

  1. class MyQueue {
  2. Stack<Integer> stack1 ;
  3. Stack<Integer> 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. dumpstackIn();
  13. return stack2.pop();
  14. }
  15. public int peek() {
  16. dumpstackIn();
  17. return stack2.peek();
  18. }
  19. public boolean empty() {
  20. return stack1.isEmpty() && stack2.isEmpty();
  21. }
  22. private void dumpstackIn() {
  23. if (!stack2.isEmpty()) return;
  24. while (!stack1.isEmpty()) {
  25. stack2.push(stack1.pop());
  26. }
  27. }
  28. }
  29. /**
  30. * Your MyQueue object will be instantiated and called as such:
  31. * MyQueue obj = new MyQueue();
  32. * obj.push(x);
  33. * int param_2 = obj.pop();
  34. * int param_3 = obj.peek();
  35. * boolean param_4 = obj.empty();
  36. */

题目 :225. 用队列实现栈

思路

  

题解

1. 使用两个Queue  LinedList实现
  1. class MyStack {
  2. Queue<Integer> que1;
  3. Queue<Integer> que2;
  4. public MyStack() {
  5. que1 = new LinkedList<>();
  6. que2 = new LinkedList<>();
  7. }
  8. public void push(int x) {
  9. que2.offer(x);
  10. while (!que1.isEmpty()) {
  11. que2.offer(que1.poll());
  12. }
  13. Queue<Integer> tempQue;
  14. tempQue = que1;
  15. que1 = que2;
  16. que2 = tempQue;
  17. }
  18. public int pop() {
  19. return que1.poll();
  20. }
  21. public int top() {
  22. return que1.peek();
  23. }
  24. public boolean empty() {
  25. return que1.isEmpty();
  26. }
  27. }
  28. /**
  29. * Your MyStack object will be instantiated and called as such:
  30. * MyStack obj = new MyStack();
  31. * obj.push(x);
  32. * int param_2 = obj.pop();
  33. * int param_3 = obj.top();
  34. * boolean param_4 = obj.empty();
  35. */
2. Queue   ArrayDeque 处理push。将que1队列中剩余的数+新数导到que2再执行一遍反转即栈的逻辑
  1. class MyStack {
  2. Queue<Integer> que1;
  3. Queue<Integer> que2;
  4. public MyStack() {
  5. que1 = new ArrayDeque<>();
  6. que2 = new ArrayDeque<>();
  7. }
  8. public void push(int x) {
  9. while (que1.size() > 0) {
  10. que2.add(que1.poll());
  11. }
  12. que1.add(x);
  13. while (que2.size() > 0) {
  14. que1.add(que2.poll());
  15. }
  16. }
  17. public int pop() {
  18. return que1.poll();
  19. }
  20. public int top() {
  21. return que1.peek();
  22. }
  23. public boolean empty() {
  24. return que1.isEmpty();
  25. }
  26. }
  27. /**
  28. * Your MyStack object will be instantiated and called as such:
  29. * MyStack obj = new MyStack();
  30. * obj.push(x);
  31. * int param_2 = obj.pop();
  32. * int param_3 = obj.top();
  33. * boolean param_4 = obj.empty();
  34. */
3.使用双端队列解决。为了实现单头需要将数据从左边导出到右边(或者从右到左,只要逻辑顺序没错就行)
  1. class MyStack {
  2. Deque<Integer> deq;
  3. public MyStack() {
  4. deq = new ArrayDeque<>();
  5. }
  6. public void push(int x) {
  7. deq.addFirst(x);
  8. }
  9. public int pop() {
  10. int size = deq.size();
  11. while (size-- > 0) {
  12. deq.addFirst(deq.peekLast());
  13. deq.pollLast();
  14. }
  15. return deq.pollFirst();
  16. }
  17. public int top() {
  18. return deq.peekFirst();
  19. }
  20. public boolean empty() {
  21. return deq.isEmpty();
  22. }
  23. }
  24. /**
  25. * Your MyStack object will be instantiated and called as such:
  26. * MyStack obj = new MyStack();
  27. * obj.push(x);
  28. * int param_2 = obj.pop();
  29. * int param_3 = obj.top();
  30. * boolean param_4 = obj.empty();
  31. */

 4.使用一个Queue  LinkedList实现。对于push每次都把新的数翻转到最后。

  1. class MyStack {
  2. Queue<Integer> que;
  3. public MyStack() {
  4. que = new LinkedList<>();
  5. }
  6. public void push(int x) {
  7. que.offer(x);
  8. int size = que.size();
  9. while (size-- > 1) {
  10. que.offer(que.poll());
  11. }
  12. }
  13. public int pop() {
  14. return que.poll();
  15. }
  16. public int top() {
  17. return que.peek();
  18. }
  19. public boolean empty() {
  20. return que.isEmpty();
  21. }
  22. }
  23. /**
  24. * Your MyStack object will be instantiated and called as such:
  25. * MyStack obj = new MyStack();
  26. * obj.push(x);
  27. * int param_2 = obj.pop();
  28. * int param_3 = obj.top();
  29. * boolean param_4 = obj.empty();
  30. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/185554
推荐阅读
相关标签
  

闽ICP备14008679号