为啥效果依然不是很好。。。
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)
执行用时 :2428 ms, 在所有 python3 提交中击败了14.41%的用户
内存消耗 :18.1 MB, 在所有 python3 提交中击败了6.00%的用户
执行用时为 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