生活随笔
收集整理的這篇文章主要介紹了
剑指offer全套题解:Python版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 二叉樹的鏡像
class Solution:def invertTree(self
, root
: Optional
[TreeNode
]) -> Optional
[TreeNode
]:if root
is None:returnself
.invertTree
(root
.left
)self
.invertTree
(root
.right
)root
.left
, root
.right
= root
.right
, root
.left
return root
鏈表中環的入口結點
class Solution:def EntryNodeOfLoop(self
, pHead
):if pHead
is None or pHead
.next is None:return Noneslow
= fast
= pHead
while fast
is not None and fast
.next is not None:slow
= slow
.next fast
= fast
.next.next if slow
== fast
:fast
= pHead
while fast
!= slow
:slow
= slow
.next fast
= fast
.nextreturn fast
return None
刪除鏈表中重復的結點
在這里插入代碼片
從尾到頭打印鏈表
class Solution:def printListFromTailToHead(self
, listNode
: ListNode
) -> List
[int]:res
= []while listNode
:res
.append
(listNode
.val
)listNode
= listNode
.next return res
[::-1]
斐波那契數列
在這里插入代碼片
跳臺階
在這里插入代碼片
變態跳臺階
在這里插入代碼片
矩形覆蓋
在這里插入代碼片
把字符串轉換成整數
在這里插入代碼片
平衡二叉樹
在這里插入代碼片
和為S的連續正數序列
在這里插入代碼片
左旋轉字符串
在這里插入代碼片
數字在排序數組中出現的次數
在這里插入代碼片
數組中只出現一次的數字
在這里插入代碼片
翻轉單詞順序列
在這里插入代碼片
二叉樹的深度
在這里插入代碼片
和為S的兩個數字
在這里插入代碼片
順時針打印矩陣
在這里插入代碼片
二叉樹的下一個結點
在這里插入代碼片
對稱的二叉樹
在這里插入代碼片
把二叉樹打印成多行
在這里插入代碼片
按之字形順序打印二叉樹
在這里插入代碼片
序列化二叉樹
在這里插入代碼片
二叉搜索樹的第k個結點
在這里插入代碼片
數據流中的中位數
在這里插入代碼片
重建二叉樹
在這里插入代碼片
滑動窗口的最大值
在這里插入代碼片
用兩個棧實現隊列
在這里插入代碼片
旋轉數組的最小數字
在這里插入代碼片
丑數
在這里插入代碼片
兩個鏈表的第一個公共結點
class Solution:def FindFirstCommonNode(self
, pHead1
, pHead2
):if pHead1
is None or pHead2
is None:return Noneset1
= []while pHead1
:set1
.append
(pHead1
)pHead1
= pHead1
.nextwhile pHead2
:if pHead2
in set1
:return pHead2pHead2
= pHead2
.nextreturn None
第一個只出現一次的字符位置
在這里插入代碼片
數組中的逆序對
在這里插入代碼片
連續子數組的最大和
在這里插入代碼片
最小的K個數
在這里插入代碼片
數組中出現次數超過一半的數字
在這里插入代碼片
整數中1出現的次數(從1到n整數中1出現的次數)
在這里插入代碼片
把數組排成最小的數
在這里插入代碼片
數組中重復的數字
在這里插入代碼片
構建乘積數組
在這里插入代碼片
二維數組中的查找
在這里插入代碼片
撲克牌順子
在這里插入代碼片
孩子們的游戲(圓圈中最后剩下的數)
在這里插入代碼片
正則表達式匹配
在這里插入代碼片
表示數值的字符串
在這里插入代碼片
字符流中第一個不重復的字符
在這里插入代碼片
替換空格
在這里插入代碼片
矩陣中的路徑
在這里插入代碼片
機器人的運動范圍
在這里插入代碼片
求1+2+3+…+n
在這里插入代碼片
不用加減乘除做加法
在這里插入代碼片
二叉搜索樹與雙向鏈表
在這里插入代碼片
復雜鏈表的復制
在這里插入代碼片
字符串的排列
在這里插入代碼片
二進制中1的個數
在這里插入代碼片
鏈表中倒數第k個結點
class Solution:def FindKthToTail(self
, pHead
: ListNode
, k
: int) -> ListNode
:head
= cur
= pHead
for i
in range(k
):if cur
is None:return Nonecur
= cur
.next while cur
:cur
= cur
.nexthead
= head
.nextreturn head
合并兩個排序的鏈表
class Solution:def Merge(self
, pHead1
: ListNode
, pHead2
: ListNode
) -> ListNode
:dummy
= head
= ListNode
(0) while pHead1
and pHead2
:if pHead1
.val
> pHead2
.val
:head
.next = pHead2head
= head
.nextpHead2
= pHead2
.nextelse:head
.next = pHead1head
= head
.nextpHead1
= pHead1
.nexthead
.next = pHead1
or pHead2
return dummy
.next
反轉鏈表
class Solution:def ReverseList(self
, head
: ListNode
) -> ListNode
:pre
= Nonehead
= head
while head
:temp
= head
.next head
.next = pre pre
= headhead
= temp
return pre
樹的子結構
在這里插入代碼片
數值的整數次方
在這里插入代碼片
調整數組順序使奇數位于偶數前面
在這里插入代碼片
包含min函數的棧
在這里插入代碼片
二叉樹中和為某一值的路徑
在這里插入代碼片
從上往下打印二叉樹
在這里插入代碼片
二叉搜索樹的后序遍歷序列
在這里插入代碼片
棧的壓入、彈出序列
在這里插入代碼片
總結
以上是生活随笔為你收集整理的剑指offer全套题解:Python版的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。