日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用栈实现队列 Implement Queue using Stacks

發布時間:2023/11/29 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用栈实现队列 Implement Queue using Stacks 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

問題:

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

  • You must use?only?standard operations of a stack -- which means only?push to top,?peek/pop from top,?size, and?is emptyoperations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

解決:

【注】關于測試參數的解釋:

["MyQueue","push","push","peek","pop","pop","empty"] --- 表示要進行的操作

[[],[1],[2],[],[],[],[]] --- 表示對應傳入的參數

① 使用兩個棧s1,s2,進棧的時候不做任何處理,出棧的時候把棧逆序放在另外一個棧,出另外一個棧。
之前寫經常出現空棧異常或者超時,就是沒有寫好s1,s2之間的轉換。

class MyQueue { // 87ms
? ? Stack<Integer> s1 = new Stack<>();
? ? Stack<Integer> s2 = new Stack<>();
? ? /** Initialize your data structure here. */
? ? public MyQueue() {}
? ? /** Push element x to the back of queue. */
? ? public void push(int x) {
? ? ? ? while(! s2.isEmpty()) s1.push(s2.pop());
? ? ? ? s1.push(x);

? ? }
? ? /** Removes the element from in front of queue and returns that element. */
? ? public int pop() {
? ? ? ? while(! s1.isEmpty()) s2.push(s1.pop());
? ? ? ? return s2.pop();

? ? }
? ? /** Get the front element. */
? ? public int peek() {
? ? ? ? while(! s1.isEmpty()) s2.push(s1.pop());
? ? ? ? return s2.peek();
? ? }
? ? /** Returns whether the queue is empty. */
? ? public boolean empty() {
? ? ? ? while(! s2.isEmpty()) s1.push(s2.pop());
? ? ? ? return s1.isEmpty();

? ? }
}
/**
?* Your MyQueue object will be instantiated and called as such:
?* MyQueue obj = new MyQueue();
?* obj.push(x);
?* int param_2 = obj.pop();
?* int param_3 = obj.peek();
?* boolean param_4 = obj.empty();
?*/

② 使用兩個棧,在進棧的時候,把棧逆序放在另外一個棧,出的時候直接出另外一個棧就可以了。

class MyQueue { //93ms
? ? Stack<Integer> s1 = new Stack<>();
? ? Stack<Integer> s2 = new Stack<>();
? ? /** Initialize your data structure here. */
? ? public MyQueue() {}
? ? /** Push element x to the back of queue. */
? ? public void push(int x) {
? ? ? ? while(! s1.isEmpty()) s2.push(s1.pop());
? ? ? ? s2.push(x);
? ? ? ? while(! s2.isEmpty()) s1.push(s2.pop());

? ? }
? ? /** Removes the element from in front of queue and returns that element. */
? ? public int pop() {
? ? ? ? return s1.pop();
? ? }
? ? /** Get the front element. */
? ? public int peek() {
? ? ? ? return s1.peek();
? ? }
? ? /** Returns whether the queue is empty. */
? ? public boolean empty() {
? ? ? ? return s1.isEmpty();
? ? }
}

③ 除了使用兩個棧之外,額外使用一個指針指向頭部。

class MyQueue { //113ms
? ? Stack<Integer> s1 = new Stack<>();
? ? Stack<Integer> s2 = new Stack<>();
? ? int head;
? ? /** Initialize your data structure here. */
? ? public MyQueue() {}
? ? /** Push element x to the back of queue. */
? ? public void push(int x) {
? ? ? ? if(s1.isEmpty()) head = x;
? ? ? ? s1.push(x);
? ? }
? ? /** Removes the element from in front of queue and returns that element. */
? ? public int pop() {
? ? ? ? while(! s1.isEmpty()) s2.push(s1.pop());
? ? ? ? int top = -1;
? ? ? ? if(! s2.isEmpty()){
? ? ? ? ? ? top = s2.pop();
? ? ? ? ? ? if(! s2.isEmpty()) head = s2.peek();
? ? ? ? }
? ? ? ? while(! s2.isEmpty()) s1.push(s2.pop());
? ? ? ? return top;
? ? }
? ? /** Get the front element. */
? ? public int peek() {
? ? ? ? return head;
? ? }
? ? /** Returns whether the queue is empty. */
? ? public boolean empty() {
? ? ? ? return s1.isEmpty();
? ? }
}

轉載于:https://my.oschina.net/liyurong/blog/1512615

總結

以上是生活随笔為你收集整理的使用栈实现队列 Implement Queue using Stacks的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。