鍚堝苟鐨勮壓鏈?.0锛氬悎骞堕摼琛ㄣ€佹暟缁勩€佷簩鍙夋爲
Posted BU绉戠爺閫?/a> 鍚堝苟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍚堝苟鐨勮壓鏈?.0锛氬悎骞堕摼琛ㄣ€佹暟缁勩€佷簩鍙夋爲相关的知识,希望对你有一定的参考价值。
鐩綍
1 鍚堝苟K涓湁搴忛摼琛?(鍏?
2 鍚堝苟鏈夊簭鏁扮粍
3 鍚堝苟浜屽弶鏍?/span>
1 鍚堝苟K涓湁搴忛摼琛?/span>
濂戒簡锛岄偅鐜板湪璇蜂綘灏嗗涓摼琛ㄥ悎骞跺埌涓€涓崌搴忛摼琛ㄤ腑锛岃繑鍥炲悎骞跺悗鐨勯摼琛?/span>
绀轰緥1锛?/span>
杈撳叆锛歭ists = [[1,4,5],[1,3,4],[2,6]]
杈撳嚭锛歔1,1,2,3,4,4,5,6]
1->4->5,
1->3->4,
2->6
馃憞
1->1->2->3->4->4->5->6
绀轰緥2锛?/span>
杈撳叆锛歭ists = [] 鎴?[[]]
杈撳嚭锛歔]
1.1 鍥為【涓婄瘒鏂囩珷鏂规硶鈥斺€斿垎娌?/span>
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
return self.merge(lists, 0, len(lists) - 1)
def merge(self, lists, left, right):
# 鍒嗘不锛氬綊骞剁殑鎬濊矾
if left == right:
return lists[left]
elif left > right:
return None
else:
mid = (left + right) // 2
return self.mergeTwoLists(self.merge(lists, left, mid), self.merge(lists, mid + 1, right))
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
# 鏈€灏忓瓙闂锛氬悎骞?涓摼琛?/span>
if not l1:
return l2
elif not l2:
return l1
elif l1.val < l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
1.2 鍚堝苟鍚庢帓搴?/span>
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def mergeKLists(self, lists):
nums = []
for node in lists:
while node: # 鐩存帴鐢╳hile node灏卞彲浠?/span>
nums.append(node.val)
node = node.next
nums.sort()
p = head = ListNode(0) # 铏氭嫙澶磋妭鐐?/span>
for value in nums:
p.next = ListNode(value) # 鍥哄畾娴佺▼
p = p.next
return head.next
2 鍚堝苟鏈夊簭鏁扮粍
鍚堝苟鏈夊簭鏁扮粍A鍜孊锛屽厓绱犱釜鏁板垎鍒负m锛宯
2.1 涓€琛屾悶瀹?/span>
class Solution:
def merge(self, A, m, B, n):
A[:] = sorted(A[:m]+B2)
2.2 璁剧疆鍝ㄥ叺锛?span class="mq-167"> Infinity鈥斺€?nbsp;鈭?/span>
class Solution:
def merge(self, A, m, B, n):
A_copy = A[:m]
A[:] = []
A_copy.append(float('inf'))
B.append(float('inf'))
i = j = 0
for k in range(m + n):
if A_copy[i] < B[j]:
A.append(A_copy[i])
i += 1
else:
A.append(B[j])
j += 1
3 鍚堝苟浜屽弶鏍?/span>
鍚堝苟涓轰竴涓柊鐨勪簩鍙夋爲銆傚悎骞剁殑瑙勫垯鏄鏋滀袱涓妭鐐归噸鍙狅紝閭d箞灏嗕粬浠殑鍊肩浉鍔犱綔涓鸿妭鐐瑰悎骞跺悗鐨勬柊鍊硷紝鍚﹀垯涓嶄负 NULL 鐨勮妭鐐瑰皢鐩存帴浣滀负鏂颁簩鍙夋爲鐨勮妭鐐广€?/span>绀轰緥锛?/span>
3.1 娣卞害浼樺厛鎼滅储锛?/span>
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
if not t1:
return t2
if not t2:
return t1
t = TreeNode(t1.val + t2.val)
t.left = self.mergeTrees(t1.left, t2.left)
t.right = self.mergeTrees(t1.right, t2.right)
return t
3.2 骞垮害浼樺厛鎼滅储锛?/span>
鎺屾彙BFS鐨勬€濊矾鍚庯紝骞朵笉澶嶆潅
class Solution:
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
if not t1: return t2
if not t2: return t1
merged = TreeNode(t1.val + t2.val)
# deque 鏄弻杈归槦鍒楋紙double-ended queue锛夛紝鍏锋湁闃熷垪鍜屾爤鐨勬€ц川
queue = collections.deque([merged])
que1 = collections.deque([t1])
que2 = collections.deque([t2])
while que1 or que2:
# merged姘歌繙鎸囧悜鏍癸紝node鍒欒礋璐i亶鍘?/span>
node = queue.popleft()
node1 = que1.popleft()
node2 = que2.popleft()
left1, right1 = node1.left, node1.right
left2, right2 = node2.left, node2.right
if left1 or left2:
if left1 and left2:
node.left = TreeNode(left1.val + left2.val)
queue.append(node.left)
que1.append(left1)
que2.append(left2)
elif left1:
node.left = left1
elif left2:
node.left = left2
if right1 or right2:
if right1 and right2:
node.right = TreeNode(right1.val + right2.val)
queue.append(node.right)
que1.append(right1)
que2.append(right2)
elif right1:
node.right = right1
elif right2:
node.right = right2
return merged
Hello BU learner
娆㈣繋鍏虫敞鎴戯紝涓€璧峰涔狅紝涓€璧疯繘姝?
璺熺潃BU绉戠爺閫氾紝瀛︿範璺笂鏇磋交鏉俱€?/p>
以上是关于鍚堝苟鐨勮壓鏈?.0锛氬悎骞堕摼琛ㄣ€佹暟缁勩€佷簩鍙夋爲的主要内容,如果未能解决你的问题,请参考以下文章