Why the effect is still not very good. . .
class Solution: def numComponents(self, head: ListNode, G: List[int]) -> int: if head.next==None: if head.val in G: return 1 else: return 0 s=0 r=[] p=head while p: if p.val in G: s+=1 p=p.next if p==None: r.append(s) else: if s!=0: r.append(s) s=0 p=p.next return len(r)
When execution: 2428 ms, beat the 14.41% of users in all python3 submission
Memory consumption: 18.1 MB, beat the 6.00% of users in all python3 submission
执行用时为 96 ms 的范例 # 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: is_started = False count = 0 G = set(G) while head: if head.val in G: is_started = True if head.next is None: count += 1 else: if is_started: count += 1 is_started = False head = head.next return count
——2019.10.24