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, less than 93 %
完成日期:05/14/2019
关键点:HashSet
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int numComponents(ListNode head, int[] G) {
Set<Integer> set = new HashSet<>();
int result = 0; for(int num : G)
{
set.add(num);
} for(ListNode node = head; node != null; node = node.next)
{
if(set.contains(node.val)
&& (node.next == null || !set.contains(node.next.val)))
result++;
} return result;
}
}
参考资料:LeetCode Discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 817. Linked List Components (链表组件)的更多相关文章
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- (链表 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 ...
- 817. Linked List Components - LeetCode
Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- [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】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] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
随机推荐
- Delphi Close、Halt、terminate、ExitProcess的区别
Close:1.只关闭本窗体2.当Close是一个主窗体时,程序会退出.3.Close会发生FormClose事件,FormCloseQuery事件4.主窗体close以后程序就Application ...
- 工程师技术(二):postfix基础邮件服务、postfix空客户端邮件服务、搭建mariadb数据库系统、配置一个数据库、使用数据库查询
一.postfix基础邮件服务 目标: 本例要求在虚拟机server0上配置 postfix 基础服务,具体要求如下: 1> 监听本机的所有接口 2> 将邮件域和邮件服务主机名都改为 ...
- Java-Class-E:org.springframework.http.HttpStatus
ylbtech-Java-Class-E:org.springframework.http.HttpStatus 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 1. /* * C ...
- HTML-参考手册: Px、Em 换算工具
ylbtech-HTML-参考手册: Px.Em 换算工具 1.返回顶部 1. Px.Em 换算工具 以下工具提供了em和px的换算工具. 第一个输入框:设置了网页默认的字体像素 (通常 16px) ...
- AI应用在金融领域,如何能够在商业上有所突破
AI应用在金融领域,如何能够在商业上有所突破 如今,随着社会不断发展,技术不断进步,国内外各大金融机构已经在大数据.人工智能.区块链等新技术上有很多尝试,智能客服.智能投顾等新金融形式也早已不新鲜.那 ...
- 浏览器获取自定义响应头response-headers
原创作品版权归属本人所有,违者必究 https://blog.csdn.net/qq_37025445/article/details/82888731想在浏览器获取响应头里面自定义的响应头:file ...
- 在Windows上安装部署Cuckoo
1. Cuckoo使用的第三方工具及库 Yara:http://plusvic.github.io/yara/ Pydeep:https://github.com/kbandla/pydeep Yar ...
- python输入一个\输出2个\问题
在Python里面,如果\后面不是一个合法的转移字符,那么,Python会打印两个\,换句话说,Python将\也当成普通字符看待,而不是转义符的标志: >>>S = 'C:\py\ ...
- spring-boot 集成ehcache报错:org.springframework.expression.spel.SpelEvaluationException: EL1008E:
错误信息:org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'pageab ...
- 结对编程UI
GitHub:https://github.com/zsl1996/UI/commits/master 一. 实验内容 这是交付给最终用户的软件,有一定的界面和必要的辅助功能.完 ...