-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path817.py
42 lines (39 loc) · 1.24 KB
/
817.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 104 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def numComponents(self, head: ListNode, G: List[int]) -> int:
G = set(G)
c = 0
mxc = 0
while head:
if head.val in G:
c = 1
else:
mxc += c
c=0
head = head.next
mxc+=c
return mxc
__________________________________________________________________________________________________
sample 108 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def numComponents(self, head: ListNode, G: List[int]) -> int:
s = set(G)
cnt = 0
# only need first one
while head:
if head.val in s and (not head.next or head.next.val not in s):
cnt += 1
head = head.next
return cnt
__________________________________________________________________________________________________