Q:
给定一个链表(链表结点包含一个整型值)的头结点 head
。
同时给定列表 G
,该列表是上述链表中整型值的一个子集。
返回列表 G
中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 G
中)构成的集合。
示例 1:
输入: head: 0->1->2->3 G = [0, 1, 3] 输出: 2 解释: 链表中,0 和 1 是相连接的,且 G 中不包含 2,所以 [0, 1] 是 G 的一个组件,同理 [3] 也是一个组件,故返回 2。
示例 2:
输入: head: 0->1->2->3->4 G = [0, 3, 1, 4] 输出: 2 解释: 链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。
链接:https://leetcode-cn.com/problems/linked-list-components/
思路:
代码:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def numComponents(self, head, G):
"""
:type head: ListNode
:type G: List[int]
:rtype: int
"""
if not head:
return 0
tmp = []
while head:
tmp.append(head.val)
head = head.next
slow = 0
res = 0
while slow < len(tmp):
if tmp[slow] in G:
res += 1
fast = slow + 1
while fast < len(tmp):
if tmp[fast] in G:
fast += 1
else:
break
slow = fast
else:
slow += 1
return res
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def numComponents(self, head, G):
"""
:type head: ListNode
:type G: List[int]
:rtype: int
"""
ret=0
G = set(G)
while head:
add=False
while head and head.val in G:
add=True
head=head.next
if add:
ret+=1
else:
head = head.next
return ret