1. """
  2. We are given head, the head node of a linked list containing unique integer values.
  3.  
  4. We are also given the list G, a subset of the values in the linked list.
  5.  
  6. Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list.
  7.  
  8. Example 1:
  9.  
  10. Input:
  11. head: 0->1->2->3
  12. G = [0, 1, 3]
  13. Output: 2
  14. Explanation:
  15. 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
  16.  
  17. Example 2:
  18.  
  19. Input:
  20. head: 0->1->2->3->4
  21. G = [0, 3, 1, 4]
  22. Output: 2
  23. Explanation:
  24. 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
  25.  
  26. """
  27. class Solution:
  28. def numComponents(self, head, G):
  29. set_G = set(G) #试了用list也能通过,set是为了规范数据集
  30. p = head
  31. count =0
  32. while(p):
  33. if p.val in set_G and (p.next == None or p.next!=None and p.next.val not in set_G):
  34. #!!!if条件句关键
  35. #将G中的元素放入set 或者字典中用于查找。
  36. # 遍历链表, 链表中的元素被计数的条件是,
  37. # 如果当前元素在G中且下一个元素不在G中(或者为None),
  38. # 那么当前的元素属于一个component。
  39. count += 1
  40. p = p.next
  41. return count

leetcode817 Linked List Components的更多相关文章

  1. 817. Linked List Components - LeetCode

    Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...

  2. [Swift]LeetCode817. 链表组件 | Linked List Components

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  3. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  4. (链表 set) leetcode 817. Linked List Components

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  5. 817. Linked List Components

    1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...

  6. #Leetcode# 817. Linked List Components

    https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...

  7. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode 817. Linked List Components (链表组件)

    题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...

  9. 算法与数据结构基础 - 链表(Linked List)

    链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...

随机推荐

  1. lua叠代器

    注意:叠待值遇到nil就退出 叠代器,是符合for遍历框架,需要满足条件 1-叠代函数,常量,控制变量 2-叠代函数可以接受二个参数,当然也可以忽略处理(利用闭包封装参数作为控制变量和状态变量) 无状 ...

  2. JS闭包(1)

    1.首先看一段代码: var a = 1; function fn1(){ var b = 2; function fn2(){ console.log(a); console.log(b); } } ...

  3. windows安装ActiveMQ以及点对点以及发布订阅

    一.MQ产品的分类 1.RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量级,更适合于企业级 ...

  4. Python学习 —— 实现简单的爬虫

    为了加快学习python3.x,查了许多资料后写了这个脚本,这个脚本主要是爬取百度图片'东方幻想乡'的图片,但还是有很多问题存在. 下面给出代码: # 更新了一下代码 from urllib impo ...

  5. 代码审计变成CTF

    0x01 代码审计中的信息收集 一个cms代码量确实不少,通读代码耗时长,效果也不一定好.而一个功能点如果之前出过漏洞,特别是多次出现漏洞的地方,证明开发者对这个漏洞的理解不充分,很容易再次绕过补丁. ...

  6. CSP-J2019 纪念品

    Description: Solution: 第一天买入,第二天卖出,在干些别的,再把第二天刚卖出的再买回来,就相当于是啥也没干.也就是说手中的物品本身要算在手中的钱中.这也就是为什么 dp 的状态可 ...

  7. 解决 U2000 R017 安装报错: 检查SQL server数据库环境变量信息 ( 异常 ) [ 详细信息 ] PATH环境变量中缺少数据库路径的信息

    U2000 R017 安装报错: 检查SQL server数据库环境变量信息 ( 异常 ) [ 详细信息 ] PATH环境变量中缺少数据库路径的信息 管理员模式打开注册表位置: HKEY_LOCAL_ ...

  8. js加密(十)csti.cn md5

    1. http://www.csti.cn/index.htm 2. 登录密码加密 3. 加密js: var hexcase = 0; var b64pad = ""; var c ...

  9. Python3---常见函数---range()用法

    0X01函数说明: python range() 函数可创建一个整数列表,一般用在 for 循环中. 0X02函数语法: range(start,stop[,step]) start: 计数从 sta ...

  10. 记一次关于NVROM中遇到的“Could not prepare Boot variable:No space left on device”问题的解决历程

    注:关于我电脑遇到的问题,不是一两句话能够说清楚的.为了能够比较完整的呈现问题的某些细节,在这篇博客中我会添加许多问题发生的背景,如果当中有观点与您的三观不合,请立即停止阅读,及时止损. 注:此篇文章 ...