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 ...
随机推荐
- sql 链接查询
连接查询是另一种类型的多表查询.连接查询对多个表进行JOIN运算,简单地说,就是先确定一个主表作为结果集,然后,把其他表的行有选择性地“连接”在主表结果集上. 例如,我们想要选出students表的所 ...
- console.log(([])?true:false); console.log(([]==false?true:false)); console.log(({}==false)?true:false)
下面是题目的类型转换结果: Boolean([]); //true Number([]); //0 Number({}); // NaN Number(false); //0 因此: console. ...
- delphi 时间
DELPHI高精度计时方法 //取毫秒级时间精度(方法一): var t1,t2:int64; r1:int64; begin t1:=GetTickCount;//获取开始计数 WINDOWS AP ...
- 制作linux根文件系统
转载地址:http://wenku.baidu.com/view/2cb1b4707fd5360cba1adb14.html 2011十一月 24 转 嵌入式根文件系统制作 (3985) (0) 转自 ...
- C++——指针与引用
1.指针本身为对象,引用只是对象的别名.故有指针的引用,没有引用的引用,没有引用的指针.指针必须指向一个实际的对象.引用也必须是实际对象的别名. 2.允许指针赋值和拷贝,指针可指向不同的对象 3.指针 ...
- Linux内存管理(深入理解Linux内核)
Linux的内存管理,实际上是借助80x86的硬件分段和分页电路,将逻辑地址转化为物理地址的. 物理内存中,有一部分是一直(Permanently)映射给内核使用的,这部分主要用于保存内核的代码,以及 ...
- 面向对象(四)——classmethod、staticmethod装饰器(绑定方法与非绑定方法)
classmethod.staticmethod装饰器 一.绑定方法与非绑定方法 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): (1) 绑定到类的方法:用classmethod ...
- 020_JUC
JUC Java.util.Concurrent 并发包 池的顶级接口 Executor 子接口 ExecutorService 工具类 Executors(Collections.Arrays .. ...
- C语言typedef
#include <stdio.h> //基本类型 typedef int MyInt; //可以对typedef产生的类型名二次起别名 typedef MyInt MyInt2; // ...
- C语言结构体内存分配详情
#include <stdio.h> int main() { /*************************************************** * * 结构体内存 ...