"""
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. git push的时候报错: Out of memory, malloc failed (tried to allocate 82037333 bytes)

    原因:上传的文件过大,这里我上传的文件有10G+所以报了上面的错误 解决方法:依次运行:git config --global pack.threads 1 git,git config --glob ...

  2. swoole 监控文件改动

    <?php /** * 场景: * 进程监控文件改动 */ date_default_timezone_set('PRC'); echo '进程id:' . posix_getpid() . P ...

  3. python学习 —— seaborn、matplotlib、pandas、numpy package的混合使用

    这里使用了Titanic Machine learning数据集,然后通过Seaborn的函数来拟合和绘制回归线,matplotlib进行可视化. 先来一个简单的测试: import pandas a ...

  4. java各种面试问题

    二.Java多线程相关 线程池的原理,为什么要创建线程池?创建线程池的方式: 线程的生命周期,什么时候会出现僵死进程: 说说线程安全问题,什么实现线程安全,如何实现线程安全: 创建线程池有哪几个核心参 ...

  5. mysql将一个表拆分成多个表(一)(转载)

    转载 直接根据数据量进行拆分 有一个5000条数据的表,要把它变成没1000条数据一个表的5等份. 假设:表名:xuesi 主键:kidxuesi共有5000条数据,kid从1到5000自动增长题目: ...

  6. [多校联考]SLON!!!

    题目描述 $SLON$是一个调皮的学生,为了让他静下心来,老师给他出了一道数学题:给定表达式$A$,$A$中含有变量$x$和$+,-,*,(,)$这些符号,括号成对出现,一个算术运算符均对应两个操作数 ...

  7. USACO January Contest Gold Time is Mooney 题解

    题意 给出一个有向图,走到每个节点有 \(m_i\) 的收益,每一条边要走一天,走 \(T\) 天的花费是 \(C\cdot T^2\),求从节点 \(1\) 开始并且在节点 \(1\) 结束的旅行的 ...

  8. Acwing897 最长公共子序列

    题目大意:求两个字符串的最长公共子序列的长度. 分析:这是一个典型的dp入门题,LCS. 代码: #include<bits/stdc++.h> using namespace std; ...

  9. Keras入门——(4)长短期记忆网络LSTM(一)

    参考: https://blog.csdn.net/zwqjoy/article/details/80493341 https://blog.csdn.net/u012735708/article/d ...

  10. pytorch 调整tensor的维度位置

    target.permute([0, 3, 1, 2]) 一定要使用permute以及中括号 一些在我这里没起到作用的网上的例子: 1. https://blog.csdn.net/zouxiaolv ...