#Leetcode# 817. Linked List Components
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 byhead
,1 <= 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的更多相关文章
- (链表 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 ...
- LeetCode 817. Linked List Components (链表组件)
题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 817. Linked List Components
1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- [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 ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
随机推荐
- 20155229 2016-2017-2 《Java程序设计》第九周学习总结
20155229 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 JDBC(Java DataBaseConnectivity)即java数据库连接 ...
- vba 批量生成条形图代码
Sub hong3()'' 宏3 宏d Dim a, b As Integer Dim str As String For a = 227 To 947 Step 15 b = a + 5 str = ...
- 2_C语言中的数据类型 (三)原码、反码、补码
1.1 原码 将最高位做为符号位(0代表正,1代表负),其余各位代表数值本身的绝对值 +7的原码是00000111 -7的原码是10000111 +0的原码是00000000 -0的原码是 ...
- 单元测试时 出现找不到类或者 NoClassDefFoundError 的问题
这种情况下,启动或重启下服务器即可
- 黑白表格样式教师求职简历免费word模板
10款精黑白表格样式教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度 ...
- 【TestNG测试】TestNG、Maven、testng.xml构建测试工程
创建一个maven工程 使用Idea创建maven工程 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...
- python自动化17-JS处理滚动条
前言 selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 常见场景: 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. 这时候 ...
- Docker Manager for Docker Swarm deploy
一.Swarm概述 Swarm是Docker公司在2014年12月初发布的一套较为简单的工具,用来管理Docker集群,它将一群Docker宿主机变成一个单一的,虚拟的主机.Swarm使用标准的Doc ...
- 廖雪峰git教程学习笔记2
本地git仓库和github仓库之间的传输是通过SSH加密的,所以: 注册GitHub账号 创建SSH key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id ...
- cp命令详解
基础命令学习目录首页 http://man.linuxde.net/cp 如果把一个文件复制到一个目标文件中,而目标文件已经存在,那么,该目标文件的内容将被破坏.此命令中所有参数既可以是绝对路径名,也 ...