1. 程式人生 > >【Leetcode】232. 用棧實現佇列

【Leetcode】232. 用棧實現佇列

題目描述:

使用棧實現佇列的下列操作:

  • push(x) -- 將一個元素放入佇列的尾部。
  • pop() -- 從佇列首部移除元素。
  • peek() -- 返回佇列首部的元素。
  • empty() -- 返回佇列是否為空。

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);  
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false

說明:

  • 你只能使用標準的棧操作 -- 也就是隻有 push to toppeek/pop from top
    size, 和 is empty 操作是合法的。
  • 你所使用的語言也許不支援棧。你可以使用 list 或者 deque(雙端佇列)來模擬一個棧,只要是標準的棧操作即可。
  • 假設所有操作都是有效的 (例如,一個空的佇列不會呼叫 pop 或者 peek 操作)。

解題思路:

棧——先進後出;佇列——先進先出。對比一下,我們就知道只要改變棧的入棧位置(或者出棧位置),佇列和棧可以相互轉換的。我們把先入棧的資料通過臨時棧存入棧底,那麼就是一個隊列了。

AC程式碼:


class MyQueue {
public:
	/** Initialize your data structure here. */
	MyQueue() {}

	/** Push element x to the back of queue. */
	void push(int x) 
	{
		std::stack<int> temp_stack; 
		while (!data_stack.empty())  
		{
			temp_stack.push(data_stack.top());  
			data_stack.pop();
		}
		temp_stack.push(x);   
		while (!temp_stack.empty())
		{
			data_stack.push(temp_stack.top());  
			temp_stack.pop();
		}
	}

	/** Removes the element from in front of queue and returns that element. */
	int pop() 
	{
		int x = data_stack.top();
		data_stack.pop();
		return x;
	}

	/** Get the front element. */
	int peek() 
	{
		return data_stack.top();
	}

	/** Returns whether the queue is empty. */
	bool empty() 
	{
		return data_stack.empty();
	}
private:
	std::stack<int> data_stack;
};

/**
 * 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();
 * bool param_4 = obj.empty();
 */

相關推薦

Leetcode232. 實現佇列

題目描述: 使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new M

LeetCode 簡單題62-實現佇列

宣告: 今天是第62道題。使用棧實現佇列的相關操作。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` )) 正文 題目:使用棧實現佇列的下列操作:

LeetCode題解232_實現佇列(Implement-Queue-using-Stacks)

目錄 描述 解法一:在一個棧中維持所有元素的出隊順序 思路 入隊(push) 出隊(pop) 檢視隊首(peek) 是否為空(empty) Java 實現 Python 實現 解法二:一

LeetCode 232. 實現佇列

程式碼:class MyQueue { public: /** Initialize your data structure here. */ stack<int> s1, s2; MyQueue() { }

[leetcode]Python實現-232.實現佇列

232.用棧實現佇列 描述 使用棧實現佇列的下列操作: push(x) – 將一個元素放入佇列的尾部。 pop() – 從佇列首部移除元素。 peek() – 返回佇列首部的元素。 empty() – 返回佇列是否為空。

232. 實現佇列(JavaScript)

使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new MyQueue

Leetcode252. 佇列實現

題目描述: 使用佇列實現棧的下列操作: push(x) -- 元素 x 入棧 pop() -- 移除棧頂元素 top() -- 獲取棧頂元素 empty() -- 返回棧是否為空 注意: 你只能使用佇列的基本操作-- 也就是 push to back, peek/

LeetCode題解232_實現隊列(Implement-Queue-using-Stacks)

復雜 彈出 兩個棧 art 分析 完成後 棧操作 all n) 目錄 描述 解法一:在一個棧中維持所有元素的出隊順序 思路 入隊(push) 出隊(pop) 查看隊首(peek) 是否為空(empty) Java 實現 Python 實現 解法二:一個棧入,一個棧出

LeetCode#232-Implement Queue using Stacks-實現佇列

一、題目 使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new MyQueue(); queue.push(

劍指offer兩個實現佇列

用兩個棧來實現一個佇列,完成佇列的Push和Pop操作。 佇列中的元素為int型別。 public class Solution {        Stack<Integer> stack1 = new Stack<In

Leetcode __232. 實現佇列

問題描述 使用棧實現佇列的下列操作: push(x) – 將一個元素放入佇列的尾部。 pop() – 從佇列首部移除元素。 peek() – 返回佇列首部的元素。 empty() – 返回佇列是否為空。 示例: MyQueue queue = new MyQue

[leetcode]實現佇列[javascript]

描述 使用棧實現佇列的下列操作: push(x) – 將一個元素放入佇列的尾部。 pop() – 從佇列首部移除元素。 peek() – 返回佇列首部的元素。 empty() – 返回佇列是否為空。 示例: MyQueue queue = ne

C語言實現反轉陣列實現)51nod

題幹: 輸入一個長度為n(1 <= n <= 100000)陣列,倒序輸出他。 陣列中的元素ai滿足(1 <= ai <= 100000)。 Input 第一行一個整數n

LeetCode232之實現佇列(Implement Queue using Stacks)

一、題目 二、一種解題思路 1)介紹:雙棧實現佇列法   方法解析:使用兩個棧做為基礎,一個棧儲存輸入的元素,另外一個棧將前面棧中的元素儲存到自己中,這樣就實現了佇列的效果,最先進的元素在in棧的棧底,out棧的棧頂。 ()從一個棧到另外一個棧的操作,僅在out

LeetCode232 ·實現佇列(C++)

題目描述: 使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new MyQueue()

佇列&//實現佇列

使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new MyQue

LeetCode18. 4Sum - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given an array nums of n integers and an integer target, are there eleme

LeetCode15. 3Sum - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given an array nums of n integers, are there elements a, b, c in nums su

LeetCode232實現佇列c++

 使用棧實現佇列的下列操作: push(x) -- 將一個元素放入佇列的尾部。 pop() -- 從佇列首部移除元素。 peek() -- 返回佇列首部的元素。 empty() -- 返回佇列是否為空。 示例: MyQueue queue = new

內功基礎演算法——佇列

導語: 下面兩個連結是我的leetcode棧和佇列的分類。 棧:https://www.cnblogs.com/zhangwanying/p/9886577.html (共40題) 佇列:https://www.cnblogs.com/zhangwanying/p/9886581.html(共