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

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.

代码:

/**
* 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) {
int ans = 0;
unordered_set<int> s(G.begin(), G.end()); while(head) {
if(s.count(head -> val) && (!head -> next || !s.count(head -> next -> val)))
ans ++; head = head -> next;
}
return ans;
}
};

  找在 G 中有多少个链表的子链表 把 G 里面的数字存到 unordered_set 中 (为了快速查找节点的数值是不是在数组中用 HashSet)

对于链表的数值 如果当前的在数组中且当前节点之后没有节点或者当前节点的下一个节点的值不存在在数组中那么出现一个新的子链表 所以 ans ++ 结果 return ans

#Leetcode# 817. Linked List Components的更多相关文章

  1. (链表 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 ...

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

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

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

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

  7. [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 ...

  8. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  9. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

随机推荐

  1. BSGS算法总结

    BSGS算法总结 \(BSGS\)算法(Baby Step Giant Step),即大步小步算法,用于解决这样一个问题: 求\(y^x\equiv z\ (mod\ p)\)的最小正整数解. 前提条 ...

  2. 【python3】拷贝U盘文件

    一.起因 前天在公众号上,看到一篇如何用python偷偷拷贝别人U盘内容的文章推送,感觉这个想法挺有意思的,可惜是用的是linux系统,而且移动硬盘的盘符也是写死的,不够灵活,于是就自己动手写了一个d ...

  3. 【转载】完成C++不能做到的事 - Visitor模式

    原文: 完成C++不能做到的事 - Visitor模式 拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据 ...

  4. AGC 018 F - Two Trees

    F - Two Trees 链接 题意: 给定两棵都是N个节点的有根树,节点均从1~N标号.给每个标号定一个权值(类似一号点的权值是x,那么两棵树中1号点的权值都是x),使在两棵树满足以任意节点为根的 ...

  5. linux 之 jq

    1.安装 mac 安装: brew install jq centos 安装: yum install jq ubuntu: 安装: apt-get install jq 2.使用 cat test. ...

  6. Tomcat部署Web应用

    在Tomcat中部署Web有三种方法: 1,可以将Web应用文件直接复制到webapps目录下,也可以将Web应用打成war包放到webapps目录下,tomcat会自动解开war包,并在webapp ...

  7. vs2015新功能和其他

    1.自2011版本开始开发式编译器发布:Roslyn,其提供的API可以侦测dll编译过程中访问和分析编译数据,传统的编译器都是黑盒子:源代码从黑盒子的一端进入,目标文件/程序集从另一端出来. 2.最 ...

  8. LeetCode 193. Valid Phone Numbers

    分析 难度 易 来源 https://leetcode.com/problems/valid-phone-numbers/ 题目 Given a text file file.txt that con ...

  9. 快速部署 Kubeadm 1.13 集群(ETCD)

    软件环境清单 kubeadm.x86_64  Version :1.13.1-0 kubelet.x86_64 Version : 1.13-1-0 kubectl.x86_64 Version : ...

  10. 带你轻而易举的学习python——八皇后问题

    首先我们来看一下这个著名的八皇后问题 八皇后问题:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. 在这个问题提出之后人们又将 ...