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

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     int numComponents(ListNode* head, vector<int>& G) {
12         unordered_set<int> set;
13         for(int num: G){
14             set.insert(num);
15         }
16
17         int count = 0;
18         bool prev = false;
19         ListNode* curr = head;
20         while(curr){
21             if(set.find(curr->val)!=set.end()){
22                 if(!prev){
23                     count++;
24                     prev = true;
25                 }
26             }else{
27                 prev = false;
28             }
29
30             curr= curr->next;
31         }
32         return count;
33     }
34 };

原文地址:https://www.cnblogs.com/ruisha/p/9483897.html

时间: 2024-07-30 18:58:42

817. Linked List Components的相关文章

817. Linked List Components - Medium

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

#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 componen

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

题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 linked list, 利用 hashset 来检查目前的点 和 下一个点 是否在G 里面. 如果目前的点在G里面,下一个点不在,说明这里断开了.具体看code. Java Solution: Runtime:  7 ms, faster than 81 % Memory Usage: 40 MB, l

leetcode817 Linked List Components

1 """ 2 We are given head, the head node of a linked list containing unique integer values. 3 4 We are also given the list G, a subset of the values in the linked list. 5 6 Return the number of connected components in G, where two values ar

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

EBT 道客巴巴的加密与破解 - 实用组合工具箱

本文系列共有三篇,前两篇链接在此: < EBT 道客巴巴的加密与破解 -免费下载器的基础> <EBT 道客巴巴的加密与破解 序章> 写到这里有点忧伤,有两点,一是CSDN在删除我之前上传的附件,二是doc88.com服务器端没有完成破解,也就是说我只解开了doc88客户端.不想说什么,纯贴个代码就走. AIR 项目主文件: <?xml version="1.0" encoding="utf-8"?> <s:WindowedA

Learning OpenCV Lecture 6 (Extracting Lines,Contours, and Components)

In this chapter, we will cover: Detecting image contours with the Canny operator Detecting lines in images with the Hough transform Fitting a line to a set of points Extracting the components' contours Computing components' shape descriptors Detectin

Facebook React 和 Web Components(Polymer)对比优势和劣势

目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - explanation DSLs 的种类 - 解释 Data binding 数据绑定 Native vs. VM 原生对决 VM(虚拟机) 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 React 相比 WebComponents 有一些"先天不足"之处,列举

【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 React 相比 WebComponents有一些“先天不足”之处,列举如下: 原生浏览器支持 原生语法支持(意即不把样式和结构混杂在 JS 中) 使用 Shadow DOM 封装样式 数据的双向绑定 这些都是确然的.不过他还是希望听听大家的看法,于是就有了这篇精彩的回答. 需要说明的是,这篇回答并没有讨