题目标签: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 (链表组件)的更多相关文章

  1. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  2. (链表 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 ...

  3. #Leetcode# 817. Linked List Components

    https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...

  4. 817. Linked List Components - LeetCode

    Question 817. Linked List Components Solution 题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元 ...

  5. [LeetCode] Design Linked List 设计链表

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  6. [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 ...

  7. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 817. Linked List Components

    1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...

  9. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

随机推荐

  1. Vue学习笔记【5】——如何定义一个基本的Vue代码结构

    插值表达式{{}} 和 v-text 默认 v-text 是没有闪烁问题的: v-text会覆盖元素中原本的内容,但是 插值表达式只会替换自己的这个占位符,不会把 整个元素的内容清空 v-cloak ...

  2. delphi 文件夹操作

    文件的拖放和打开拖拽 user shellapi type TForm1 = class(TForm) ListView1: TListView; procedure FormCreate(Sende ...

  3. vue2 开发总结

    vue-cli学习资料: http://m.php.cn/article/394750.html  或 https://www.cnblogs.com/zhanglin123/p/9270051.ht ...

  4. SQL比较时间查询语句

    select * from table1 where datediff(mm,'2009-8-12 13:17:50', date)>0 select * from table1 select ...

  5. 自己写Linux module来收集buddy info

    1 编写代码pslist.c 1: #include<linux/init.h> 2: #include<linux/module.h> 3: #include<linu ...

  6. tomcat服务器和HTTP协议

    tomcat:一个服务器的服务器软件,发布资源要用的 服务器组成: 1.服务器硬件 2.服务器软件 3.项目(一堆资源的集合) 4.资源tomcat本身是一个java程序,必须依赖jre运行eclip ...

  7. ASP.Net 第一天笔记 MVC 控制器与视图数据传递注意事项

    1.如果方法的参数的名称与表单元素Name属性的值一致的话,会自动填充 2.如果表单元素的Name属性与实体类型中属性一致,那么表单中的数据会自动赋值给实体中的属性 3.控制器中重载的方法 方法前上边 ...

  8. vue中:key 和react 中key={} 的作用,以及ref的特性?

    vue中:key 和react 中key={} 为了给 vue 或者react 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性 一句话概括就是 ...

  9. nashorn中js数组转为对象的问题

    背景 在项目中,使用jdk中的nashorn执行javascript脚本,例如如下脚本片段: let ctx = session.ctx; ctx.confirm = { //车牌划分后的数组 seg ...

  10. Mybatis_环境搭建

    1.配置pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...