"""
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. Struts2.0笔记二

    Mvc与servlet 1.1   Servlet的优点 1.  是mvc的基础,其他的框架比如struts1,struts2,webwork都是从servlet基础上发展过来的.所以掌握servle ...

  2. scp 常用命令总结

    1, 上传本地文件到服务器:scp /path/local_filename username@servername:/path 例如:例如scp /var/www/test.php  codingl ...

  3. 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle系统调优

    --修改 alter system set large_pool_size=64m; --显示 show parameter large_pool_size; select sum(getmisses ...

  4. 工作脚本拆分xml文并重定向数据

    sed -n '/<N/p' CM-ENB-SRVIDENTIFYBASEBSRTDD-2C-ALLV2.9.0-20191209020003.xml.gz.xml|awk -F"&g ...

  5. 解题报告:luogu P5543 [USACO19FEB]The Great Revegetation S

    题目链接:P5543 [USACO19FEB]The Great Revegetation S 好坑啊,都身败名裂了. 思路一: 考虑染色法,跑一遍搜所就好了,不给代码了. 思路二: 考虑并查集,我想 ...

  6. 在Centos下单机部署kubernetes

    官方安装手册 https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/ ...

  7. Codeforces1307D. Cow and Fields

    对于本题,最短路,考虑bfs,那么我们可以跑2次bfs,求出每个点到1与n的最短路,设为x_a, x_b,那我们可以把问题转换成max(min{x_a+y_b,x_b+y_a}+1)(x,y属于1到n ...

  8. OPCDA通信--工作在透明模式下的CISCO ASA 5506-X防火墙配置

    尊重原创,转发请声名 inside OPCSERVER 一台 outside OPCCLIENT 一台 route模式 配置没成功,放弃,采用透明模式 !----进入全局配置-- configure ...

  9. MySQL帮助文档的使用

    帮助文档使用 在 MySQL 使用过程中,可能经常会遇到以下问题: 某个操作语法忘记了,需要快速查找. 当前版本上,某个字段类型我们想快速知道它的取值范围? 当前版本上,都支持哪些函数?希望有例子能快 ...

  10. kill和raise

    kill向特定的进程和进程组发送信号 raise向进程自身发送信号