劍指offer刷題記錄

2021-10-24 05:51:40 字數 4263 閱讀 3783

遞迴法:鍊錶的後續遍歷,並用self.k來記錄倒數節點的位置,找到了就返回找到的節點,否則返回none

# -*- coding:utf-8 -*-

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def __init__(self):

self.k = 0

def findkthtotail(self, head, k):

# write code here

self.k = k

def recursion(head):

if head == none:

return none

res = recursion(head.next)

if res != none:

return res

self.k = self.k - 1

if self.k == 0:

return head

return none

return recursion(head)

棧輔助法:利用棧出棧倒序的機制來找到倒數第k個鍊錶節點(注意臨界值和特殊情況)

# -*- coding:utf-8 -*-

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def findkthtotail(self, head, k):

# write code here

if k == 0:

return none

stack =

while head:

head = head.next

index = 0

while stack and index < k-1:

stack.pop()

index += 1

return stack[-1] if len(stack) > 0 else none

鍊錶的後序遍歷,遞迴反轉

# -*- coding:utf-8 -*-

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

# 返回listnode

def reverselist(self, phead):

# write code here

if not phead:

return none

# newhead

if phead.next == none:

return phead

# newhead

newhead = self.reverselist(phead.next)

phead.next.next = phead

phead.next = none

return newhead

迭代翻轉

# -*- coding:utf-8 -*-

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

# 返回listnode

def reverselist(self, phead):

# 處理特殊情況

if phead == none:

return none

# 初始化第乙個要被翻轉的節點和其前驅

cur = phead

pre = none

# 開始迭代翻轉,並更新cur和pre

while cur:

# 先記錄下下乙個將要被翻轉的node

nxt = cur.next

cur.next = pre

pre = cur

cur = nxt

return pre

迭代法(需要乙個空節點來輔助)

# -*- coding:utf-8 -*-

class listnode:

def __init__(self, x):

self.val = x

self.next = none

class solution:

# 返回合併後列表

def merge(self, phead1, phead2):

# 新建乙個空的head

head = listnode(0)

p = head

p1 = phead1

p2 = phead2

while p1 and p2:

if p1.val > p2.val:

# 連線

p.next = p2

# 更新p

p = p2

# 移動p2

p2 = p2.next

else:

p.next = p1

p = p1

p1 = p1.next

# 將剩餘的那邊的後續節點接到p的後面

if p1 or p2:

if p1:

p.next = p1

else:

p.next = p2

return head.next

遞迴法

# -*- coding:utf-8 -*-

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

# 返回合併後列表

def merge(self, phead1, phead2):

# 只要有一邊節點為空了就終止遞迴, 返回結果

if not phead2:

return phead1

elif not phead1:

return phead2

if phead1.val > phead2.val:

phead2.next = self.merge(phead1, phead2.next)

return phead2

else:

phead1.next = self.merge(phead1.next, phead2)

return phead1

一開始我用的是求兩個相同的數進行處理,發現不對,原因是子樹

正確的解答在這:

# -*- coding:utf-8 -*-

# class treenode:

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

class solution:

def hassubtree(self, proot1, proot2):

# 注意這個函式不能採用求相同樹

def recur(a, b):

# b是空的話可以直接返回true (b是a的子樹)

if not b: return true

if not a or a.val != b.val: return false

return recur(a.left, b.left) and recur(a.right, b.right)

return bool(proot1 and proot2) and (recur(proot1, proot2) or self.hassubtree(proot1.left, proot2) or self.hassubtree(proot1.right, proot2))

劍指 offer 刷題記錄

任誰都躲不過找工作的問題,好希望能多準備一些時間,奈何時間不等人,每天刷幾道題,並且記錄下來吧 def replacespace s write code here num space 0 new s for i in range len s if i num space 1 for i in ra...

劍指offer刷題記錄 綜合

將乙個字串轉換成乙個整數,要求不能使用字串轉換整數的庫函式。數值為0或者字串不是乙個合法的數值則返回0 輸入描述 輸入乙個字串,包括數字字母符號,可以為空 輸出描述 如果是合法的數值表達則返回該數字,否則返回0 做這個題目做的真的很煩,最麻煩的就是判斷當前是否越界。可儲存的最大的正數末位為7,可儲存...

Leetcode刷題記錄 劍指offer

面試題3 陣列中重複數字 使用set,時間複雜度o n 空間複雜度o n class solution object deffindrepeatnumber self,nums type nums list int rtype int a set for num in nums if num in ...