剑指offer python 博客园_python-剑指offer16-20
16、樹
操作給定的二叉樹,將其變換為源二叉樹的鏡像。
#class TreeNode:#def __init__(self, x):#self.val = x#self.left = None#self.right = None
classSolution:#返回鏡像樹的根節點
defMirror(self, root):#write code here
if root ==None:returnNoneif root.left != None or root.right !=None:
root.left,root.right=root.right,root.left
self.Mirror(root.left)
self.Mirror(root.right)
17、數組
輸入一個矩陣,按照從外向里以順時針的順序依次打印出每一個數字,例如,如果輸入如下4 X 4矩陣: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 則依次打印出數字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
方法一:
#-*- coding:utf-8 -*-
classSolution:#matrix類型為二維列表,需要返回列表
defprintMatrix(self, matrix):#write code here
res=[]
i,j=0,0
d=0
vis=[[0 for _ in range(len(matrix[0]))] for _ inrange(len(matrix))]
tmp=[[0,1],[1,0],[0,-1],[-1,0]]while i>=0 and i=0 and j
res.append(matrix[i][j])
vis[i][j]=1
while i+tmp[d][0]>=0 and i+tmp[d][0]=0 and j+tmp[d][1]
i+=tmp[d][0]
j+=tmp[d][1]
res.append(matrix[i][j])
vis[i][j]=1d=(d+1)%4i+=tmp[d][0]
j+=tmp[d][1]returnres
方法二:
#-*- coding:utf-8 -*-
classSolution:#matrix類型為二維列表,需要返回列表
defprintMatrix(self, matrix):#write code here
if notmatrix:returnmatrix
rows=len(matrix)
cols=len(matrix[0])
result=[]
start=0#判斷是否繼續往里走一圈
while cols > start * 2 and rows > start * 2:#走一圈的代碼
end_x = cols - 1 -start
end_y= rows - 1 -start#打印第一行
for i in range(start, end_x+1):
result.append(matrix[start][i])#打印最后一列
if start
result.append(matrix[i][end_x])#打印最后一行
if start < end_x and start
result.append(matrix[end_y][i])#打印第一列
if start < end_x and start < end_y - 1:for i in range(end_y-1, start, -1):
result.append(matrix[i][start])
start+= 1
returnresult
方法三:
#-*- coding:utf-8 -*-
classSolution:#matrix類型為二維列表,需要返回列表
defprintMatrix(self, matrix):if notmatrix:returnmatrix
result=[]while len(matrix) >0:
result.extend(matrix[0])
matrix= zip(*matrix[1:])[::-1]return result
18、棧
定義棧的數據結構,請在該類型中實現一個能夠得到棧中所含最小元素的min函數(時間復雜度應為O(1))
#-*- coding:utf-8 -*-
classSolution:def __init__(self):
self.s1=[]
self.s2=[]defpush(self, node):#write code here
self.s1.append(node)if len(self.s2)==0:
self.s2.append(node)else:if node>self.s2[-1]:
self.s2.append(self.s2[-1])else:
self.s2.append(node)defpop(self):#write code here
if len(self.s1)==0:returnNonedel self.s2[-1]
res=self.s1[-1]del self.s1[-1]returnresdeftop(self):#write code here
if len(self.s1)==0:returnNoneelse:return self.s1[-1]defmin(self):#write code here
if len(self.s1)==0:returnNoneelse:return self.s2[-1]
較好的思路:開一個新棧,此棧與數值棧一一對應,在元素入棧時,就記錄處于每一個位置時的當前最小值,這樣不論什么情況,不論之前如何出棧入棧,都能立即獲取到最小值。
19、棧
輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否可能為該棧的彈出順序。假設壓入棧的所有數字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對應的一個彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。(注意:這兩個序列的長度是相等的)
classSolution:defIsPopOrder(self, pushV, popV):#write code here
stack =[]
cur=0for pop inpopV:while len(stack)==0 or pop != stack[-1] and cur
stack.append(pushV[cur])
cur+=1
if cur == len(pushV) and stack[-1] !=pop:returnFalseelse:
stack.pop()return True
解題思路:例如對于壓入順序 12345和彈出順序45312,首先讀到4,則從壓入序列里依次壓入1234,然后彈出4;讀到5,則壓入5,彈出5;讀到3,棧頂恰好為3,彈出3;讀到1,棧頂為2,應當繼續壓入直到壓入1,但壓入序列已經讀完,沒找到1,所以錯誤。
20、樹
從上往下打印出二叉樹的每個節點,同層節點從左至右打印。
#-*- coding:utf-8 -*-#class TreeNode:#def __init__(self, x):#self.val = x#self.left = None#self.right = None
classSolution:#返回從上到下每個節點值列表,例:[1,2,3]
defPrintFromTopToBottom(self, root):#write code here
if notroot:return[]
stack=[root]
res=[]whilestack:for i inrange(len(stack)):
t=stack.pop(0)
res.append(t.val)ift.left:
stack.append(t.left)ift.right:
stack.append(t.right)returnres
解題思路:樹的層次遍歷,注意的是當root為空時要返回[],而不是None。
總結
以上是生活随笔為你收集整理的剑指offer python 博客园_python-剑指offer16-20的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做一个虎牙的根管治疗要多少钱
- 下一篇: 补牙填充物是什么材料