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.

Note:

  • If N is the length of the linked list given by head1 <= N <= 10000.
  • The value of each node in the linked list will be in the range [0, N - 1].
  • 1 <= G.length <= 10000.
  • G is a subset of all values in the linked list.

这道题给了我们一个链表,又给了我们一个结点值数组,里面不一定包括了链表中所有的结点值。让我们返回结点值数组中有多少个相连的组件,因为缺失的结点值会将原链表断开,实际上就是让我们求有多少个相连的子链表,题目中给的例子很好的说明题意。这道题并不需要什么特别高深的技巧,难懂的算法,直接按题目的要求来找就可以了。首先,为了快速的在结点值数组中查找某个结点值是否存在,我们可以将所有的结点值放到一个HashSet中,这样我们就能在常数级的时间复杂度中查找。然后我们就可以来遍历链表了,对于遍历到的每个结点值,我们只有两种情况,在或者不在HashSet中。不在HashSet中的情况比较好办,说明此时断开了,而在HashSet中的结点,有可能是该连续子链表的起始点,或者是中间的某个点,而我们的计数器对该子链表只能自增1,所以我们需要想办法来hanlde这种情况。博主最先想到的办法是先处理不在HashSet中的结点,处理方法就是直接跳到下一个结点。那么对于在HashSet中的结点,我们首先将计数器res自增1,然后再来个循环,将之后所有在集合中的结点都遍历完,这样才不会对同一个子链表多次增加计数器,参见代码如下:

解法一:

class Solution {
public:
int numComponents(ListNode* head, vector<int>& G) {
int res = ;
unordered_set<int> nodeSet(G.begin(), G.end());
while (head) {
if (!nodeSet.count(head->val)) {
head = head->next;
continue;
}
++res;
while (head && nodeSet.count(head->val)) {
head = head->next;
}
}
return res;
}
};

我们可以稍稍修改代码,使其更加简洁,我们在遍历的时候进行判断,如果当前结点在集合中,并且当前结点是尾结点或者下一个结点不在集合中的时候,我们让计数器自增1,通过这种操作,我们不会多加也不会漏加计数器,参见代码如下:

解法二:

class Solution {
public:
int numComponents(ListNode* head, vector<int>& G) {
int res = ;
unordered_set<int> nodeSet(G.begin(), G.end());
while (head) {
if (nodeSet.count(head->val) && (!head->next || !nodeSet.count(head->next->val))) {
++res;
}
head = head->next;
}
return res;
}
};

参考资料:

https://leetcode.com/problems/linked-list-components/description/

https://leetcode.com/problems/linked-list-components/solution/

https://leetcode.com/problems/linked-list-components/discuss/123842/C++JavaPython-Easy-and-Concise-Solution-with-Explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Linked List Components 链表组件的更多相关文章

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

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

  2. Java实现 LeetCode 817 链表组件(暴力)

    817. 链表组件 给定一个链表(链表结点包含一个整型值)的头结点 head. 同时给定列表 G,该列表是上述链表中整型值的一个子集. 返回列表 G 中组件的个数,这里对组件的定义为:链表中一段最长连 ...

  3. 817. Linked List Components - LeetCode

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

  4. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  5. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  6. Linked List Sorting (链表)

    Linked List Sorting (链表)   A linked list consists of a series of structures, which are not necessari ...

  7. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  8. Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件

    一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Components Manually——手动配置componen ...

  9. LeetCode:删除排序链表中的重复元素【83】

    LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...

随机推荐

  1. MongoDB 3.6.9 集群搭建 - 切片+副本集

    1. 环境准备 在Mongo的官网下载Linux版本安装包,然后解压到对应的目录下:由于资源有限,我们采用Replica Sets + Sharding方式来配置高可用.结构图如下所示: 这里我说明下 ...

  2. 解决 IIS 反向代理ARR URLREWRITE 设置后,不能跨域跳转 return Redirect 问题

    1.选择iis根节点,点击ARR 2.选择代理设置 3.去掉reverse rewrite host in response headers的勾选,点击应用即可

  3. JavaScript 基本类型和引用类型

    前言 ECMAScript变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 基本类型 Undefined.Null.B ...

  4. 设 $y_1(x), y_2(x)$ 是 $y''+p(x)y'+q(x)y=0$ 的两个解 ($p(x), q(x)$ 连续), 且 $y_1(x_0)=y_2(x_0)=0$, $y_1(x)\not\equiv 0$. 试证: $y_1(x)$, $y_2(x)$ 线性相关.

    设 $y_1(x), y_2(x)$ 是 $y''+p(x)y'+q(x)y=0$ 的两个解 ($p(x), q(x)$ 连续), 且 $y_1(x_0)=y_2(x_0)=0$, $y_1(x)\n ...

  5. 第30月第6天 git log

    1. git log git log 96a6f18b1e0a1b7301cb4f350537d947afeb22bc -p -1 我们常用 -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最 ...

  6. nnet3 TDNN chunk, left-context, right-context

    chunk-width 数据块的宽度 NnetIo name=="input" indexes,left-context+num-frame+right-context=5+7+6 ...

  7. 过滤器实现 (cookie认证)

    cookie用来做身份认证,非常好用,只需要设置Authentication和Authorization就行了. 但是 ,如果cookie不能用了,怎么办?  不要紧,我们也可以用过滤器进行身份认证. ...

  8. iTOP-4412开发板_驱动_adc驱动升级和测试例程

    本文档介绍 iTOP-4412 开发板的 adc 驱动的升级和测试例程.自带的驱动只能支持一路 adc,本文介绍如何修改可以支持 4 路 adc 的控制.1 硬件简介如下图所示,这是 4412 的 d ...

  9. mybatis源码笔记

    mybatis设计总览 目录结构: 通过配置文件,获取SqlSessionFactory XMLConfigBuilder 解析配置文件,获取SqlSessionFactory private sta ...

  10. nginx php-fpm开启session

    一.  权限问题,这个是发生在多数情况下 找到php session目录设置给 nginx 或者 apache最大权限 二.  配置问题按如下就没啥问题了 [Session] session.save ...