"""
We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list. Example 1: Input:
head: 0->1->2->3
G = [0, 1, 3]
Output: 2
Explanation:
0 and 1 are connected, so [0, 1] and [3] are the two connected components. Example 2: Input:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
Output: 2
Explanation:
0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components. """
class Solution:
def numComponents(self, head, G):
set_G = set(G) #试了用list也能通过,set是为了规范数据集
p = head
count =0
while(p):
if p.val in set_G and (p.next == None or p.next!=None and p.next.val not in set_G):
#!!!if条件句关键
#将G中的元素放入set 或者字典中用于查找。
# 遍历链表, 链表中的元素被计数的条件是,
# 如果当前元素在G中且下一个元素不在G中(或者为None),
# 那么当前的元素属于一个component。
count += 1
p = p.next
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. Hadoop基准测试(一)

    测试对于验证系统的正确性.分析系统的性能来说非常重要,但往往容易被我们所忽视.为了能对系统有更全面的了解.能找到系统的瓶颈所在.能对系统性能做更好的改进,打算先从测试入手,学习Hadoop主要的测试手 ...

  2. STM32单片机的软件重启和远程重启

    STM32单片机可以通过以下代码实现重启(core_cm3.h).同时如果利用AT命令进行无线通讯,服务器后台和客户端之间用MODBUS通讯即4G+MODBUS RTU,可以利用F05写单个线圈的方法 ...

  3. AJAX请求返回JSON数据动态生成html

    1:DeliveryPersonVO对象 package com.funcanteen.business.entity.delivery.vo; import java.util.List; impo ...

  4. 六、Centos7中配置svn服务器

    今天配置了 SVN 记在这儿 备忘: --svn开机自启动服务 systemctl enable svnserve.service --svn开机自启动服务 systemctl disable svn ...

  5. 如何使用Python输出一个[斐波那契数列]

    如何使用Python输出一个[斐波那契数列]Fibonacci 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonac ...

  6. 【剑指Offer面试编程题】题目1522:包含min函数的栈--九度OJ

    题目描述: 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行为一个整数n(1<=n&l ...

  7. Spring mvc mybatis 查询结果缺少字段 解决方法

    参考:https://blog.csdn.net/xiaofeifei8421/article/details/43231815

  8. CentOS配置源、wget、ifconfig基础环境

    执行命令: curl http://10.200.0.14:8000/portal.cgi -X POST -d 'username=lishuai&password=test@cetc38& ...

  9. js正则 - 限制用户名只能中文、字母和数字 , 不能包含特殊字符

    /^[\u4E00-\u9FA5A-Za-z0-9]+$/   

  10. RabbitMq学习笔记——RabbitMQ C的使用

    1.需要用到的参数: 主机名:hostname.端口号:port.交换器:exchange.路由key:routingkey .绑定路由:bindingkey.用户名:user.密码:psw,默认用户 ...