题目标签: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. PHP FILTER_VALIDATE_URL 过滤器

    定义和用法 FILTER_VALIDATE_URL 过滤器把值作为 URL 来验证. Name: "validate_url" ID-number: 273 可能的标志: FILT ...

  2. (53)C# 工具

    https://docs.microsoft.com/zh-cn/dotnet/framework/tools/ildasm-exe-il-disassembler 一.Visual Studio的开 ...

  3. ajax url地址

    当前网址 http://localhost:8080/exam_paper/402881ec5c3924ec015c394ee4210000/set_questions ajax请求url var u ...

  4. 6.1.2 The continuous assignment statement

    Frm: IEEE Std 1364™-2001, IEEE Standard Verilog® Hardware Description Language The continuous assign ...

  5. 2017 ICPC Asia Urumqi A.coins (概率DP + 期望)

    题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical c ...

  6. UVA 1525 Falling Leaves

    题目链接:https://vjudge.net/problem/UVA-1525 题目链接:https://vjudge.net/problem/POJ-1577 题目大意 略. 分析 建树,然后先序 ...

  7. pycharm中django同步数据库问题

    一.Django数据同步过程中遇到的问题: 1.raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you hav ...

  8. 框架_mybatis1

    mybatis框架是实现与数据之间交互 入门: 创建数据库环境 创建实体类与数据库对应字段 实现Serializable 创建接口定义方法 创建主配置方法: <?xml version=&quo ...

  9. WdatePicker设置时间与倒计时

    之前苦于jQuery的datetimepicker插件不知道如何设置秒数,用了同学推荐的WdatePicker,真心好用. 相关文档用法可以上http://www.my97.net/dp/index. ...

  10. metasploit5配置数据库

    #踩坑:Please run msfdb as a non-root user apt-get install postgresql #安装数据库 service postgresql start # ...