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 ...
随机推荐
- python3 获取电脑磁盘、CPU、内存使用情况
import psutil # cd C:\Python36-32\Scripts pip install psutil # 获取本机磁盘使用率和剩余空间G信息 def get_disk_info() ...
- [7.18NOIP模拟测试5]星际旅行 题解
题面(加密) 考场上靠打表yy出的规律进而想到的正解233333 可以把一条双向边拆成两条单向边,这样的话每个点度数都为偶数,符合欧拉图的定义. 那么题目可以转化为:去掉两条边,使图中存在一条欧拉路. ...
- php开发面试题---vue面试题(vue.js的好处及作用)
php开发面试题---vue面试题(vue.js的好处及作用) 一.总结 一句话总结: 双向数据绑定:在做ajax的时候,更新实在是太方便了 用数据绑定的思想,vue可以简单写单个页面,也可以写一个大 ...
- Java数据结构----集合
Java的集合可以分为两类, 第一类是以数组为代表,这类集合可以描述线性表类型的数据结构,以Collection为基类,其中自己用过或者了解的有 实现List接口的类:LinkedList,Array ...
- go gin
1.安装 go get -u github.com/gin-gonic/gin 2. package main import "github.com/gin-gonic/gin" ...
- python 读取设备的另一个方法
import time,sys templist = []#设置一个空列表,用来放设备内容deviceslist =[]#设置一个空列表,用来放分割后的设备内容devices = [] #设置一 ...
- 24. Jmeter GUI 及NON GUI实现分布式
什么是分布式: Jmeter的集群模式可以让我们将多台机器联合起来一起产生负载,从而弥补单台机器负载生成能力不足的问题. 假设我们的测试计划会产生100个threads,我们使用6台机器进行分布式测试 ...
- JAVA单元测试的用法和要点(入门篇)
一.单元测试的目的? 单元测试是编写测试代码,用以检测特定的.明确的.细颗粒的功能! 严格来说,单元测试只针对功能点进行测试,不包括对业务流程正确性的测试.现在一般公司都会进行业务流程的测 ...
- docker学习日记一(镜像构建-container commit和image build)
构建镜像的方式两种: 一.根据已有的container构建-docker container commit 二.根据已有的image构建-docker image build(推荐) containe ...
- Spring MVC源码分析(一):ContextLoaderListener的设计与实现
ContextLoaderListener在我的Spring源码分析(一):从哪里开始看spring源码这篇文章,分析过在web容器,如tomcat,启动web应用时,会通过监听器的方式,通知Serv ...