【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/linked-list-components/description/
题目描述
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 head, 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.
题目大意
给出了一个不包含重复数字的链表,和由该链表部分节点元素构成的数组。要统计出这个数组能构成链表中的多少段联通分量。
解题方法
这个题乍一看,感觉很高大上,其实就是【LeetCode】830. Positions of Large Groups 解题报告(Python)的翻版嘛。
从左到右遍历链表依次,每遇到一个节点,就看看这个节点的数值在不在G中,并且这个节点的下一个节点是不是空(末尾),下一个节点值在不在G中。
如果当前的节点值在G中,而下一个节点是空或者节点值不在G中,那么就是一个新的分段呗。
这个题第一次提交的时候超时,超时原因应该在判断一个元素在不在列表中这步比较慢。使用set之后就能提交通过了。。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def numComponents(self, head, G):
"""
:type head: ListNode
:type G: List[int]
:rtype: int
"""
groups = 0
subset = set(G)
while head:
if head.val in subset and (not head.next or head.next.val not in subset):
groups += 1
head = head.next
return groups
二刷的时候使用判断是否连续,使用的一个变量,手动维护一个变量稍嫌麻烦。
注意set的写法,可以传入开始和结束两个指针。
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(G.begin(), G.end());
ListNode* p = head;
int res = 0;
bool isCon = false;
while (p) {
if (s.count(p->val)) {
if (!isCon) {
res ++;
isCon = true;
}
} else {
isCon = false;
}
p = p->next;
}
return res;
}
};
日期
2018 年 5 月 28 日 —— 太阳真的像日光灯~
2018 年 12 月 14 日 —— 12月过半,2019就要开始
【LeetCode】817. Linked List Components 解题报告(Python & C++)的更多相关文章
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- #Leetcode# 817. Linked List Components
https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...
- (链表 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 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- 搭建简单的SpringCloud项目三:问题及解决
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...
- 多线程高级篇1 — JUC — 只弄到处理高并发集合问题
1.线程池 1.1).什么是线程池? 池( pool ),就是一个容器,所以线程池就是把多个线程对象放到一个容器中 1.2).如何创建线程池? 先来了解几个常识 Executor -- 这是一个接口( ...
- C语言中的指针与整数相加的值计算
以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数 3. 指针强制转换为另一个类型后(指针或者是整数) + 整数 测试例子: 1 struct AAA{ int a; char b[ ...
- 日常Java 2021/11/9
线程的优先级 每一个Java线程都有一个优先级,这样有助于操作系统确定线程的调度顺序.Java线程的优先级是一个整数,其取值范围是1(Thread.MIN_PRIORITY ) -10 (Thread ...
- 一起手写吧!call、apply、bind!
apply,call,bind都是js给函数内置的一些api,调用他们可以为函数指定this的执行,同时也可以传参. call call 接收多个参数,第一个为函数上下文也就是this,后边参数为函数 ...
- 【STM8】SPI通讯
这篇内容有点长,如果有人想透过我的博客学习STM8的SPI,那是我的荣幸 首先我要先说大纲,这样大家心里比较有底,可以把精力都用在SPI理解上 [SPI初步介绍]:介绍SPI如何接线.名称解释.通讯注 ...
- 创建Oracle数据库实例
创建Oracle数据库实例 转自oracle数据库创建实例 数据库已经安装完成,可以正常登陆查看用户等操作. system用户只能用normal身份登陆em.除非你对它授予了sysdba的系统权限或者 ...
- 连接 MySQL 数据库出现问题:The server time zone value ‘�й���ʱ��‘ is unrecogni....
出现问题 The server time zone value '�й���ʱ��' is unrecogni.... 解决方案 在 URL 后面加上 ?serverTimezone=UTC 如下: ...
- 【Linux】【Shell】【Basic】函数
1. 函数:function,把一段独立功能的代码当作一个整体,并为之一个名字:命名的代码段,此即为函数: 注意:定义函数的代码段不会自动执行,在调用时执行:所谓调用函数,在代码中给定函数名即可: 函 ...
- Java定时器演进过程和生产级分布式任务调度ElasticJob代码实战
为何要使用分布式任务调度 **本人博客网站 **IT小神 www.itxiaoshen.com 演示项目源码地址** https://gitee.com/yongzhebuju/spring-task ...