817. Linked List Components
1. 原始题目
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.
2. 题目理解
给定一个链表(链表结点包含一个整型值)的头结点 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 <= 10000G是链表中所有结点的值的一个子集.
3. 解题
思路:对于链表每个结点若出现在列表中,则起码是一个组件了,然后继续扫描链表看是单个元素还是有连续的元素。思路还是直观比较简单了。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def numComponents(self, head: ListNode, G: List[int]) -> int:
p = head
num = 0
while(p):
if p.val in G: # 在当前节点已经在列表中的基础上,
while p.next and p.next.val in G: # 判断下一个连续结点是否也在,即判断该组件是单个结点还是多个呢
p = p.next
num += 1
p = p.next return num
验证
# 新建链表1
listnode1 = ListNode_handle(None)
s1 = [1,2,3,4,5,6,7,8]
for i in s1:
listnode1.add(i) listnode1.print_node(listnode1.head) s = Solution()
head = s.numComponents(listnode1.head, [2,3,4,7,1])
print(head)
1 2 3 4 5 6 7 8
2
817. Linked List Components的更多相关文章
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- (链表 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
https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 817. Linked List Components (链表组件)
题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...
- [Swift]LeetCode817. 链表组件 | Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- leetcode817 Linked List Components
""" We are given head, the head node of a linked list containing unique integer value ...
- 算法与数据结构基础 - 链表(Linked List)
链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...
随机推荐
- CentOS 7 的安装
CentOS 7的安装 --------------------------- 安装前的准备: 1.去官网或是去网上下载好CentOS 7的镜像文件 下载主页: https://www.centos. ...
- MySQL常用函数介绍
MySQL常用函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.操作符介绍 1>.操作符优先级 mysql; +----------+ | +----------+ ...
- php5.4使用dblib扩展,连接sqlserver中文乱码问题
在使用php链接sqlserver的时候,查询出来的数据,编码不稳定,一会utf8,一会出现问号.很纠结.下面的方法,可以解决此种问题.前提是dblib扩展. 如果查到的结果是乱码,更改/usr/lo ...
- C语言复习---矩形法求定积分函数
一:分析: 大一学习积分的时候,我们学习过,可以通过矩形法来求定积分. 思路就是将积分区间划分成n等份,然后将这n等份近似看成矩形(或梯形),然后对所有的矩形(或梯形)的面积进行求和. 二:简单的例子 ...
- C语言复习---找出一个二维数组的鞍点
前提: 求任意的一个m×n矩阵的鞍点——鞍点是指该位置上的元素在该行上为最大.在该列上为最小, 矩阵中可能没有鞍点,但最多只有一个鞍点. m.n(<=m<=.<=n<=)及矩阵 ...
- Redis 高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
- Windows Azure中国版试用
Winodws Azure刚出来那会都没怎么关注,就在一些网页新闻上见过一些报道,之前阿里云在博客里搞过推广,拿什么豆可以换,当时也就是看了一下没去用过. 有次在一群里别人说Windows Azure ...
- 一个Silverlight工程的各文件解析
创建一个解决方案,这个解决方案包括一个ASP.NET网站项目和一个Silverlight应用程序项目. 1)ASP.net项目: -------------Default.aspx:ASP.net默认 ...
- SpringBoot系列: 理解 Spring 的依赖注入(二)
==============================Spring 容器中 Bean 的名称==============================声明 bean 有两个方式, 一个是 @B ...
- 【三】Eureka服务注册与发现
1.是什么 Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是一个基于 REST 服务,用于定位服务,以实现云端中间层服务发现和故障转移.服务注册与发现对于微服务架构来说是非常 ...