zoukankan      html  css  js  c++  java
  • 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

     
     
    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Linux忘了root的密码怎么办
    缩略图的实现
    ASP.NET程序编写注意 (转载)
    太极拳
    Linux系统管理技巧大荟萃
    茶经(转载)
    datagrid的显示控制
    太极功
    Linux下硬盘分区详解
    Tomcat4.0中文问题简单解决方法
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11735132.html
Copyright © 2011-2022 走看看