自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析數(shù)據(jù)結(jié)構(gòu)棧和隊(duì)列的相互實(shí)現(xiàn)

開發(fā) 前端
" 編程的本質(zhì)來源于算法,而算法的本質(zhì)來源于數(shù)學(xué),編程只不過將數(shù)學(xué)題進(jìn)行代碼化。"算法,一門既不容易入門,也不容易精通的學(xué)問。

[[358323]]

" 編程的本質(zhì)來源于算法,而算法的本質(zhì)來源于數(shù)學(xué),編程只不過將數(shù)學(xué)題進(jìn)行代碼化。"

算法,一門既不容易入門,也不容易精通的學(xué)問。

棧和隊(duì)列都是用來保存數(shù)據(jù)的,無論底層是使用數(shù)組還是鏈表來實(shí)現(xiàn),其基本原理是不變的,那就是棧的特點(diǎn)的先進(jìn)后出,隊(duì)列的特點(diǎn)是先進(jìn)先出。

棧 (Stack)是一種后進(jìn)先出(last in first off,LIFO)的數(shù)據(jù)結(jié)構(gòu)。線性表是用數(shù)組來實(shí)現(xiàn)的,對于棧這種只能一頭插入刪除的線性表來說,用數(shù)組下標(biāo)為0(棧底不變,只需要跟蹤棧頂?shù)淖兓纯?的一端作為棧底比較合適。


列表封裝的這些方法,實(shí)現(xiàn)棧這個常用的數(shù)據(jù)結(jié)構(gòu)比較容易。棧是一種只能在列表一端進(jìn)出的特殊列表,pop方法正好完美實(shí)現(xiàn):

  1. In [1]: stack=[1,3,5] 
  2.  
  3. In [2]: stack.append(0) # push元素0到尾端,不需要指定索引 
  4.  
  5. In [3]: stack 
  6. Out[3]: [1, 3, 5, 0] 
  7.  
  8. In [4]: stack.pop() # pop元素,不需指定索引,此時(shí)移出尾端元素 
  9. Out[4]: 0 
  10.  
  11. In [5]: stack 
  12. Out[5]: [1, 3, 5] 

由此可見Python的列表當(dāng)做棧用,完全沒有問題,push 和 pop 操作的時(shí)間復(fù)雜度都為 O(1)

隊(duì)列

隊(duì)列(Queue)則是一種先進(jìn)先出 (fisrt in first out,F(xiàn)IFO)的結(jié)構(gòu).。使用順序表存儲隊(duì)列時(shí),隊(duì)列元素的出隊(duì)是在隊(duì)頭,即下標(biāo)為0的地方,當(dāng)有元素出隊(duì)時(shí),出隊(duì)元素后面的所有元素都需要向前移動,保證隊(duì)列的隊(duì)頭始終處在下標(biāo)為0的位置,此時(shí)會大大增加時(shí)間復(fù)雜度。


使用列表模擬隊(duì)列,需要借助Python的collections模塊中的雙端隊(duì)列deque實(shí)現(xiàn)。如下模擬隊(duì)列的先進(jìn)先出,后進(jìn)后出:

  1. In [1]: from collections import deque 
  2.  
  3. In [2]: queue = [1,3,5] 
  4.  
  5. In [3]: deq = deque(queue) 
  6.  
  7. In [4]: deq.append(0)  
  8.  
  9. In [5]: deq 
  10. Out[5]: deque([1, 3, 5, 0]) # 后進(jìn)插入到隊(duì)列尾部 
  11.  
  12. In [6]: deq.popleft()   
  13. Out[6]: 1 
  14.  
  15. In [7]: deq 
  16. Out[7]: deque([3, 5, 0])# 先進(jìn)的先出 

LeetCode 第 225題:用隊(duì)列實(shí)現(xiàn)棧#使用隊(duì)列實(shí)現(xiàn)棧

  1. #使用隊(duì)列實(shí)現(xiàn)棧的下列操作:  
  2. # push(x) -- 元素 x 入棧  
  3. # pop() -- 移除棧頂元素  
  4. top() -- 獲取棧頂元素  
  5. # empty() -- 返回棧是否為空  
  6. # 注意:  
  7. # 你只能使用隊(duì)列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 這些操作是合法的。  
  8. # 你所使用的語言也許不支持隊(duì)列。 你可以使用 list 或者 deque(雙端隊(duì)列)來模擬一個隊(duì)列 , 只要是標(biāo)準(zhǔn)的隊(duì)列操作即可。  
  9. # 你可以假設(shè)所有操作都是有效的(例如, 對一個空的棧不會調(diào)用 pop 或者 top 操作)。  
  10. # Related Topics 棧 設(shè)計(jì) 

根據(jù)題意,我們只能使用隊(duì)列的基本操作,隊(duì)列因?yàn)槭窍冗M(jìn)先出,要實(shí)現(xiàn)先進(jìn)后出的棧。

無論是用隊(duì)列實(shí)現(xiàn)棧,還是用棧實(shí)現(xiàn)隊(duì)列。常見的解法方法是使用兩個隊(duì)列或者兩個棧。

假設(shè)有q1,q2兩個隊(duì)列,我們先初始化隊(duì)列。

  1. from collections import deque 
  2. class MyStack: 
  3.     def __init__(self): 
  4.         ""
  5.         Initialize your data structure here. 
  6.         ""
  7.         self.q1 = deque() 
  8.         self.q2 = deque() 

push(x) :元素 x 入棧 時(shí)和隊(duì)列添加元素的方法一樣。


壓入棧時(shí),加入到q1的末尾,那么q1末尾的元素就是棧頂元素。因此只需要append(x)即可。

  1. def push(self, x: int) -> None: 
  2.     ""
  3.     Push element x onto stack. 
  4.     ""
  5.     self.q1.append(x) 

對于pop刪除元素,我們可以使用q2保存q1的最后的元素之前的元素,然后將q1的元素進(jìn)行刪除,最后將兩個隊(duì)列進(jìn)行互換。

我們需要彈出棧頂元素,也就是q1最后的元素,隊(duì)列只能是先進(jìn)先出,我們得用q2把q1出隊(duì)的元素裝著,最后一個出隊(duì)元素就是棧頂元素。

因此,代碼需要對q1的長度進(jìn)行判斷,如果q1的長度大于1,那么將q1的頭部元素添加到q2,直到q1只有最后一個元素。

  1. def pop(self) -> int
  2.     ""
  3.     Removes the element on top of the stack and returns that element. 
  4.     ""
  5.     while len(self.q1) > 1: 
  6.         self.q2.append(self.q1.popleft()) 
  7.     tmp = self.q1.popleft() 
  8.     self.q2, self.q1 = self.q1, self.q2 
  9.     return tmp 

判斷是否為空,只需要判斷q1的隊(duì)列是否為空。

  1. def empty(self) -> bool: 
  2.     ""
  3.     Returns whether the stack is empty. 
  4.     ""
  5.     return not bool(self.q1) 

取棧頂元素。這里其實(shí)可以巧妙地解決,我們直接調(diào)用pop方法進(jìn)行刪除,在pop進(jìn)行刪除時(shí)用一個變量進(jìn)行保存,還需要對該元素重新進(jìn)行插入操作。

  1. def top(self) -> int
  2.     ans = self.pop() 
  3.     self.q1.append(ans) 
  4.     return ans 

下面就是用隊(duì)列實(shí)現(xiàn)棧完整代碼

  1. from collections import deque 
  2. class MyStack: 
  3.     def __init__(self): 
  4.         ""
  5.         Initialize your data structure here. 
  6.         ""
  7.         self.q1 = deque() 
  8.         self.q2 = deque() 
  9.  
  10.  
  11.     def push(self, x: int) -> None: 
  12.         ""
  13.         Push element x onto stack. 
  14.         ""
  15.         self.q1.append(x) 
  16.  
  17.  
  18.     def pop(self) -> int
  19.         ""
  20.         Removes the element on top of the stack and returns that element. 
  21.         ""
  22.         while len(self.q1) > 1: 
  23.             self.q2.append(self.q1.popleft()) 
  24.         tmp = self.q1.popleft() 
  25.         self.q2,self.q1 = self.q1, self.q2 
  26.         return tmp 
  27.  
  28.  
  29.     def top(self) -> int
  30.         ""
  31.         Get the top element. 
  32.         ""
  33.         ans = self.pop() 
  34.         self.q1.append(ans) 
  35.         return ans 
  36.  
  37.  
  38.     def empty(self) -> bool: 
  39.         ""
  40.         Returns whether the stack is empty. 
  41.         ""
  42.         return not bool(self.q1) 

LeetCode 第232題:用棧實(shí)現(xiàn)隊(duì)列

  1. #使用棧實(shí)現(xiàn)隊(duì)列的下列操作: 
  2. # push(x) -- 將一個元素放入隊(duì)列的尾部。  
  3. # pop() -- 從隊(duì)列首部移除元素。  
  4. # peek() -- 返回隊(duì)列首部的元素。  
  5. # empty() -- 返回隊(duì)列是否為空。 
  6. # 示例: 
  7. # MyQueue queue = new MyQueue(); 
  8. #queue.push(1); 
  9. #queue.push(2);   
  10. #queue.peek();  // 返回 1 
  11. #queue.pop();   // 返回 1 
  12. #queue.empty(); // 返回 false 
  13. # 說明: 
  14. # 你只能使用標(biāo)準(zhǔn)的棧操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。  
  15. # 你所使用的語言也許不支持棧。你可以使用 list 或者 deque(雙端隊(duì)列)來模擬一個棧,只要是標(biāo)準(zhǔn)的棧操作即可。  
  16. # 假設(shè)所有操作都是有效的 (例如,一個空的隊(duì)列不會調(diào)用 pop 或者 peek 操作)。 
  17. # Related Topics 棧 設(shè)計(jì) 

最簡單的方法使用list表示一個棧,只能使用棧的相關(guān)方法,如append(),pop(),s[-1],分別是棧頂追加元素,刪除棧頂元素,取出棧頂元素.。

  1. class MyQueue: 
  2.     def __init__(self): 
  3.         self.s = [] 
  4.     def push(self, x: int) -> None: 
  5.         self.s.append(x) 
  6.     def pop(self) -> int:     
  7.         return self.s.pop(0) 
  8.     def peek(self) -> int
  9.         return self.s[0] 
  10.     def empty(self) -> bool: 
  11.         return not bool(self.s) 

當(dāng)然也可以使用兩個棧,這個是比較復(fù)雜的操作,「但也是比較常見的算法考點(diǎn)?!?/p>

(1)初始化兩個棧結(jié)構(gòu),s1為主棧,s2為輔助棧。

(2)push往s1末尾添加元素,利用append即可實(shí)現(xiàn)。

(3)pop時(shí)候,先將s1元素向s2轉(zhuǎn)移,知道s1只剩下一個元素時(shí)候(這就是我們要返回的隊(duì)列首部元素),然后我們再把2中的元素轉(zhuǎn)移回s1中即可。

(4)返回隊(duì)列首部的元素,類似于步驟(3)的操作,唯一不同是這里我們需要將elenment先添加回stack2,然后再將stack2的元素轉(zhuǎn)移回stack1中,因?yàn)閜eek操作不需要刪除隊(duì)列首部元素

(5)empty判斷stack1尺寸即可。

出隊(duì)操作首先判斷緩存棧s2是否有元素,有的話直接取出s2棧頂元素;若s2為空并且s1中有元素,將s1中元素全部轉(zhuǎn)移到s2中,再取出s2棧頂元素,即可模擬隊(duì)列出隊(duì)操作;本例中沒有出現(xiàn)s2和s1都為空的情況。

  1. class MyQueue: 
  2.     def __init__(self): 
  3.         ""
  4.         Initialize your data structure here. 
  5.         ""
  6.         self.s1 = [] 
  7.         self.s2 = [] 
  8.          
  9.     def push(self, x: int) -> None: 
  10.         ""
  11.         Push element x to the back of queue. 
  12.         ""
  13.         self.s1.append(x) 
  14.  
  15.     def pop(self) -> int
  16.         ""
  17.         Removes the element from in front of queue and returns that element. 
  18.         ""
  19.         if self.s2: 
  20.             return self.s2.pop() 
  21.         else
  22.             if  self.s1 : 
  23.                 while self.s1: 
  24.                     self.s2.append(self.s1.pop()) 
  25.                 return self.s2.pop() 
  26.  
  27.     def peek(self) -> int
  28.         ""
  29.         Get the front element. 
  30.         ""
  31.         if self.s2: 
  32.             return self.s2[-1] 
  33.         else
  34.             if  self.s1 : 
  35.                 while self.s1: 
  36.                     self.s2.append(self.s1.pop()) 
  37.                 return self.s2[-1] 
  38.  
  39.     def empty(self) -> bool: 
  40.         ""
  41.         Returns whether the queue is empty. 
  42.         ""
  43.         if self.s1 or self.s2: 
  44.             return False 
  45.         else
  46.             return True 
  47.  
  48. # Your MyQueue object will be instantiated and called as such: 
  49. # obj = MyQueue() 
  50. # obj.push(x) 
  51. # param_2 = obj.pop() 
  52. # param_3 = obj.peek() 
  53. # param_4 = obj.empty() 

本文已收錄 GitHub:

https://github.com/MaoliRUNsen/runsenlearnpy100

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2012-05-16 17:05:33

Java數(shù)據(jù)結(jié)構(gòu)

2011-04-11 11:23:17

隊(duì)列數(shù)據(jù)結(jié)構(gòu)

2009-08-11 14:51:11

C#數(shù)據(jù)結(jié)構(gòu)與算法

2009-08-11 14:43:42

C#數(shù)據(jù)結(jié)構(gòu)與算法

2020-10-28 10:10:03

Java單鏈表數(shù)據(jù)結(jié)構(gòu)

2023-09-25 12:23:18

Python

2023-11-12 21:49:10

Redis數(shù)據(jù)庫

2022-09-01 16:27:19

JavaScriptWeb開發(fā)

2021-03-01 23:31:48

隊(duì)列實(shí)現(xiàn)棧存儲

2015-08-06 15:20:21

runtimeIOS開發(fā)

2021-03-29 08:01:20

JavaScript數(shù)據(jù)結(jié)構(gòu)

2024-01-15 06:01:36

C++數(shù)組

2021-07-16 07:57:34

Python數(shù)據(jù)結(jié)構(gòu)

2010-06-11 14:15:23

WAP協(xié)議棧

2022-08-11 08:03:43

隊(duì)列

2020-03-27 14:29:30

數(shù)據(jù)結(jié)構(gòu)

2023-10-18 17:49:58

數(shù)據(jù)結(jié)構(gòu)隊(duì)列結(jié)構(gòu)

2019-08-13 09:40:55

數(shù)據(jù)結(jié)構(gòu)算法JavasCript

2015-09-10 08:46:15

Java面試題

2021-03-09 06:30:32

JAVA數(shù)據(jù)結(jié)構(gòu)算法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號