劍指offer 分解讓複雜問題簡單化

2021-09-19 19:06:45 字數 3808 閱讀 6170

輸入乙個複雜鍊錶(每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點),返回結果為複製後複雜鍊錶的head。(注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空)

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

# class randomlistnode:

# def __init__(self, x):

# self.label = x

# self.next = none

# self.random = none

class solution:

# 返回 randomlistnode

def clone(self, phead):

# write code here

if not phead:

return none

self.clonenodes(phead)

self.clonerandom(phead)

return self.reconnectlist(phead)

def clonenodes(self, phead):

pnode = phead

while pnode:

pcloned = randomlistnode(0)

pcloned.label = pnode.label

pcloned.next = pnode.next

pnode.next = pclond

pnode = pcloned.next

def clonerandom(self, phead):

pnode = phead

while pnode:

pcloned = pnode.next

if pcloned.random:

pcloned.random = pnode.random.next

pnode = pcloned.next

def reconnectlist(self, phead):

pnode = phead

pclonednode = pclonedhead = pnode.next

pnode.next = pclonednode.next

pnode = pnode.next

while pnode:

pclonednode.next = pnode.next

pclonednode = pclonednode.next

pnode.next = pclonenode.next

pnode = pnode.next

return pclonedhead

輸入一棵二叉搜尋樹,將該二叉搜尋樹轉換成乙個排序的雙向鍊錶。要求不能建立任何新的結點,只能調整樹中結點指標的指向。

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

# class treenode:

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

class solution:

def convert(self, prootoftree):

# write code here

if not prootoftree:

return

root = phead = prootoftree

while phead.left:

phead = phead.left

self.convertcore(root)

return phead

def convertcore(self, root):

if not root.left and not root.right:

return

if root.left:

prenode = root.left

self.convertcore(root.left)

while prenode.right:

prenode = prenode.right

prenode.right = root

root.left = prenode

if root.right:

nextnode = root.right

self.convertcore(root.right)

while nextnode.left:

nextnode = nextnode.left

nextnode.left = root

root.right = nextnode

請實現兩個函式,分別用來序列化和反序列化二叉樹

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

# class treenode:

# def __init__(self, x):

# self.val = x

# self.left = none

# self.right = none

class solution:

def serialize(self, root):

# write code here

if not root:

return '#'

return str(root.val) + ',' +self.serialize(root.left) + ',' + self.serialize(root.right)

falg = -1

def deserialize(self, s):

self.flag += 1

treelist = s.split(',')

if self.flag >= len(s):

return none

root = none

if treelist[self.flag] != '#':

root = treenode(int(treelist[self.flag]))

root.left = self.deserialize(s)

root.right = self.deserialize(s)

return root

輸入乙個字串,按字典序列印出該字串中字元的所有排列。例如輸入字串abc,則列印出由字元a,b,c所能排列出來的所有字串abc,acb,bac,bca,cab和cba。

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

class solution:

def permutation(self, ss):

# write code here

if not ss:

return

if len(ss) == 1:

return list(ss)

res =

charlist = list(ss)

charlist.sort()

for i in range(len(charlist)):

if i > 0 and charlist[i] == charlist[i - 1]:

continue

temp = self.permutation(''.join(charlist[:i]) + ''.join(charlist[i + 1:]))

for j in temp:

return res

劍指offer刷題記錄 分解讓複雜問題簡單

輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 由於該鍊錶不是普通的鍊錶,複製過程中還要對特殊指標進行複製,所以我們在原煉表上操作要分多次...

35 複雜鍊錶的複製( 分解讓複雜問題簡單)

題目描述 輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 測試用例 功能測試 節點中的random指向自身 兩個節點的random形成環狀...

(複雜問題分步驟)劍指 複雜鍊錶的複製

輸入乙個複雜鍊錶 每個節點中有節點值,以及兩個指標,乙個指向下乙個節點,另乙個特殊指標指向任意乙個節點 返回結果為複製後複雜鍊錶的head。注意,輸出結果中請不要返回引數中的節點引用,否則判題程式會直接返回空 占用記憶體 9400k public class randomlistnode publi...