817. 链表组件
解法一:
class Solution:
def numComponents(self, head: ListNode, G: List[int]) -> int:
res = 0
G_set = set(G)
i = head
while i:
if i.val in G_set and (not i.next or i.next.val not in G_set):
res += 1 #找到一个组件末尾
i = i.next
return res最后更新于