817. 链表组件

给定一个链表(链表结点包含一个整型值)的头结点 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 是链表中所有结点的值的一个子集.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/linked-list-components
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法:

class Solution {
public:int numComponents(ListNode* head, vector<int>& G) {int res = 0;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;}
};

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385134.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1121 Damn Single (25 分)

"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of. Input Specification: Each input file contains one test case. For each case,…

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle&#xff08;抽奖&#xff09; for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are suppose…

987. 二叉树的垂序遍历

给定二叉树&#xff0c;按垂序遍历返回其结点值。 对位于 (X, Y) 的每个结点而言&#xff0c;其左右子结点分别位于 (X-1, Y-1) 和 (X1, Y-1)。 把一条垂线从 X -infinity 移动到 X infinity &#xff0c;每当该垂线与结点接触时&#xff0c;我们按从上到下的顺序报告结点的值…

28. 实现 strStr()

实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在&#xff0c;则返回 -1。 示例 1: 输入: haystack "hello", needle "ll" 输出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ with 0 for all i and a​k​​>0. Then N is palindromic if and only if a​i​​a​k−i​​ for all i. Zero is written 0 and is also palindrom…

1044 火星数字 (20 分)

火星人是以 13 进制计数的&#xff1a; 地球人的 0 被火星人称为 tret。地球人数字 1 到 12 的火星文分别为&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人将进位以后的 12 个高位数字分别称为&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

给定两个以字符串形式表示的非负整数 num1 和 num2&#xff0c;返回 num1 和 num2 的乘积&#xff0c;它们的乘积也表示为字符串形式。 示例 1: 输入: num1 "2", num2 "3" 输出: "6" 示例 2: 输入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一个经典的划分过程&#xff1a;我们通常采用某种方法取一个元素作为主元&#xff0c;通过交换&#xff0c;把比主元小的元素放到它的左边&#xff0c;比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列&#xff0c;请问有多少个元…

1049 数列的片段和 (20 分)

给定一个正数数列&#xff0c;我们可以从中截取任意的连续的几个数&#xff0c;称为片段。例如&#xff0c;给定数列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目录索引

序号内容1 【C Primer | 15】虚函数表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虚函数表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虚函数表剖析&#xff08;三&#xff09; 4一个C程序执行main函数前和执行完main函数后会发生什么。1 【C Priemr | 15】虚…

1046 划拳 (15 分)

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为&#xff1a;每人口中喊出一个数字&#xff0c;同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和&#xff0c;谁就赢了&#xff0c;输家罚一杯酒。两人同赢或两人同输则继续下一轮&…

多线程顺序交替打印ABCD

题目&#xff1a;按照 ABCD的顺序交替打印。 1. 测试代码&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一个只出现一次的字符

在一个字符串(0<字符串长度<10000&#xff0c;全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1&#xff08;需要区分大小写&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目录

序号题目题解标记1 43. 字符串相乘 字符串2513. 找树左下角的值二叉树3 450. 删除二叉搜索树中的节点 二叉树486. 分隔链表链表155 155. 最小栈 C题解栈77. 组合C题解回溯算法15.三数之和C题解

一个C++程序执行main函数前和执行完main函数后会发生什么。

总结&#xff1a; main函数执行之前&#xff0c;主要就是初始化系统相关资源&#xff1a; 设置栈指针初始化static静态和global全局变量&#xff0c;即data段的内容将未初始化部分的赋初值&#xff1a;数值型short&#xff0c;int&#xff0c;long等为0&#xff0c;bool为FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then N integers are…

【面试宝典 | 01】面经

字节跳动提前批后端第三面凉经该来的终究会来的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人杀&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…

算法默写

序号内容1快速排序算法2堆排序算法3归并排序算法

1149 Dangerous Goods Packaging (25 分)

When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent &#xff08;氧化剂&#xff09; must not be packed with flamma…