• leetcode——817. 链表组件


    为啥效果依然不是很好。。。

    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

     
     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    UITableview
    UIscrollview
    UITextField(详细设置)
    iOS开发UI篇—Quartz2D使用(矩阵操作)
    iOS开发UI篇—Quartz2D使用(图形上下文栈)
    类的sizeof
    Implement strStr()
    KMP很清楚的一篇解释
    Best Time to Buy and Sell Stock II
    Triangle
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11735132.html
Copyright © 2020-2023  润新知