题目标签: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

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public int numComponents(ListNode head, int[] G) {
  11. Set<Integer> set = new HashSet<>();
  12. int result = 0;
  13.  
  14. for(int num : G)
  15. {
  16. set.add(num);
  17. }
  18.  
  19. for(ListNode node = head; node != null; node = node.next)
  20. {
  21. if(set.contains(node.val)
  22. && (node.next == null || !set.contains(node.next.val)))
  23. result++;
  24. }
  25.  
  26. return result;
  27. }
  28. }

参考资料: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. AcWing 101. 最高的牛 (差分) 打卡

    有 NN 头牛站成一行,被编队为1.2.3…N,每头牛的身高都为整数. 当且仅当两头牛中间的牛身高都比它们矮时,两头牛方可看到对方. 现在,我们只知道其中最高的牛是第 PP 头,它的身高是 HH ,剩 ...

  2. NYOJ 737 (石子合并)

    该题是一道DP题,核心思想如下: 某个区间一定是这个区间内的某两个子区间合成的(这两个子区间互补,即这两个区间加起来等于大区间), 所以我们枚举所有的情况,取个最大值即可.因为最初是从2堆石子开始无法 ...

  3. appium自动化获取app的appPackage与appActivity方法总结

    一,获取apppackage 方法不止一种,我只介绍自己知道的两种. 1,通过APPIUM工具添加APK包后,会自动显示出来. 2,打开UI AUTOMATOR VIEWER 定位工具,随便指向一个定 ...

  4. 虚拟机安装(Cent OS)

    转载:http://www.cnblogs.com/kkdd-2013/p/3973807.html 0 前言 本篇主要介绍在虚拟机VMware上安装CentOS6.5的过程,并且在自己电脑上安装成功 ...

  5. CSS:CSS 组合选择符

    ylbtech-CSS:CSS 组合选择符 1.返回顶部 1. CSS 组合选择符 CSS 组合选择符 组合选择符说明了两个选择器直接的关系. CSS组合选择符包括各种简单选择符的组合方式. 在 CS ...

  6. mysql 审核

    https://javinjunfeng.top/technicalstack/database/43

  7. ThinkPHP5实用的数据库操作方法

    1.update方法总结 /** * 设置记录的某个字段值 * 支持使用数据库字段和方法 * @access public * @param string|array $field 字段名 * @pa ...

  8. LBP算子

    LBP算子特点 LBP(Local Binary Pattern),即局部二值模式,属于一种图像预处理算法,具有光照不变性和旋转不变性. 我目前做的项目是人脸表情识别,采用这种算法可以减少光照和人脸旋 ...

  9. 天道神诀--IPSAN实现多链路以及多路径安装

    # linux6 环境需求 ISCSI服务端: 2张网卡,足够的磁盘空间 iscsi客户端: 2张网卡(与服务端网段相同) ISCSI服务端配置: 与1张网卡配置主要差别在于配置文件中的允许访问网段, ...

  10. 利用VUE-CLI脚手架搭建VUE项目

    前言 在学习完vue基础语法之后,学着利用vue-cli脚手架搭建一个项目,本篇随笔主要记录搭建的过程,供大家一起学习. 具体内容 搭建vue项目的准备工作 1.安装Nodejs.NPM以及VSCod ...