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.

中文版:

给定一个链表(链表结点包含一个整型值)的头结点 head

同时给定列表 G,该列表是上述链表中整型值的一个子集。

返回列表 G 中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 G 中)构成的集合。

示例 1:

输入:
head: 0->1->2->3
G = [0, 1, 3]
输出: 2
解释:
链表中,0 和 1 是相连接的,且 G 中不包含 2,所以 [0, 1] 是 G 的一个组件,同理 [3] 也是一个组件,故返回 2。

示例 2:

输入:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
输出: 2
解释:
链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。

注意:

  • 如果 N 是给定链表 head 的长度,1 <= N <= 10000
  • 链表中每个结点的值所在范围为 [0, N - 1]
  • 1 <= G.length <= 10000
  • G 是链表中所有结点的值的一个子集.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

根据官方题解可知,利用set先把G的数据保存到一个集合中,然后遍历链表,判断所在的结点的val是否存在集合并且判断下一个结点的炸了是否不再集合中或下一个结点指向NULL,这样的话就构成不同的component了。

C++代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int numComponents(ListNode* head, vector<int>& G) {
set<int> s;
for(int x:G) s.insert(x); ListNode *cur = head;
int ans = ; while(cur){
if(s.count(cur->val) && (cur->next == NULL || !s.count(cur->next->val)))
ans++;
cur = cur->next;
}
return ans;
}
};

(链表 set) leetcode 817. Linked List Components的更多相关文章

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

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

  2. #Leetcode# 817. Linked List Components

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

  3. 817. Linked List Components - LeetCode

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

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

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

  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 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  7. (链表 双指针) leetcode 141. Linked List Cycle

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  8. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  9. [LeetCode] 141. Linked List Cycle 链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

随机推荐

  1. js的常用文档对象,document

    1.document的概念:window的子对象,由于DOM对象模型的默认对象就是window,因此Window对象中的方法和子对象不需要通过Window来引用. - 2.document的组成:属性 ...

  2. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK问题解决

    Maven构建项目报错: 解决办法: 1.eclipse菜单 -  Window - Preferences- Java - Installed JREs 将配置的JRE定位到JDK,例如JRE ho ...

  3. ftell 的使用

    ftell一般用于读取文件的长度,下面补充一个例子,读取文本文件中的内容: #include <stdio.h> #include <stdlib.h> int main() ...

  4. chrome实用快捷键速记

    标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 无痕模式下打开新窗口 Ctrl + Shift + n 打开新的标签页,并跳转到该标签页 Ctrl + t 重新打开最后关闭的标签页,并 ...

  5. Codeforces Round #540 Div. 3 F2

    考虑将每种颜色构成的极小连通块缩点,然后直接跑树形dp即可,即f[i][0/1]表示子树内是否有颜色向上延伸时删边的方案数.dp时需要去除某点的贡献,最好用前后缀积的做法而不是求逆. 至于如何缩点,假 ...

  6. HDU5745-La Vie en rose-字符串dp+bitset优化

    这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...

  7. 「POJ 1135」Domino Effect(dfs)

    BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...

  8. Hdoj 1003.Max Sum 题解

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  9. 【Linux命令】用户及分用户组

    查看用户 查看所有用户命令: cat /etc/passwd 上面命令输出内容比较杂乱,可以使用如下命令简化输出: #对于 cat /etc/passwd 的替换 cat /etc/passwd|gr ...

  10. [luogu3157][bzoj3295][CQOI2011]动态逆序对【cdq分治+树状数组】

    题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...