817. 链表组件

给定一个链表(链表结点包含一个整型值)的头结点 head。

同时给定列表 G,该列表是上述链表中整型值的一个子集。

返回列表 G 中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 G 中)构成的集合。

示例 1:

输入:

head: 0->1->2->3

G = [0, 1, 3]

输出: 2

解释:

链表中,0 和 1 是相连接的,且 G 中不包含 2,所以 [0, 1] 是 G 的一个组件,同理 [3] 也是一个组件,故返回 2。

示例 2:

输入:

head: 0->1->2->3->4

G = [0, 3, 1, 4]

输出: 2

解释:

链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。

注意:

如果 N 是给定链表 head 的长度,1 <= N <= 10000。

链表中每个结点的值所在范围为 [0, N - 1]。

1 <= G.length <= 10000

G 是链表中所有结点的值的一个子集.

PS:

可以用百度翻译看英文版,中文版少了一句话

a linked list containing unique integer values
一个包含唯一整数值的链表
/**
* 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<>();
for(int i:G){
set.add(i);
}
int res=0;
boolean flag=true;//可以加一的时候是true
while(head!=null){
if(set.contains(head.val)){
if(flag){
flag=false;
res++;
}
head=head.next;
}else{
if(!flag){
flag=true;
head=head.next;
}else{
head=head.next;
}
}
}
return res;
}
}

Java实现 LeetCode 817 链表组件(暴力)的更多相关文章

  1. Java实现 LeetCode 382 链表随机节点

    382. 链表随机节点 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶: 如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? ...

  2. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. Maxim实时时钟芯片设计指南5413-二进制编码十进制(BCD)格式实时时钟中的状态机逻辑

    网上DS12C887的文章涉及到时间的存储格式使用的都是二进制代码,究竟使用BCD码该如何操作?正好美信官网上有一篇文章.美信官网不稳定,先贴到这里,有时间再翻译. 原文链接 State Machin ...

  2. RHCSA 第一题 修改root密码

    题目要求 请修改系统的root帐号密码为redhat,确保能够使用root帐号登录系统.说明:server0 的IP为172.25.0.11/24desktop0的IP为172.25.0.10/24 ...

  3. java 实现仿照微信抢红包算法,实测结果基本和微信吻合,附demo

    实现拼手气红包算法,有以下几个需要注意的地方: 抢红包的期望收益应与先后顺序无关 保证每个用户至少能抢到一个预设的最小金额,人民币红包设置的最小金额一般是0.01元,如果需要发其他货币类型的红包,比如 ...

  4. C# Html转pdf文件

    using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; us ...

  5. python 利用 for ... else 跳出双层嵌套循环

    背景 周末在写一个爬虫时,遇到这样一种场景:从搜索结果中下载指定数量的文件 例如:搜索结果中共分为10页展示,加起来一共50条数据,现在要做的是从50条数据中下载指定数量的数据 为了实现这个功能,开始 ...

  6. java学习笔记之原型模式及深浅拷贝

    一.原型模式的基本介绍 在聊原型模式之前,我们来思考一个小问题,传统的方式我们是如何克隆对象呢? 那我们以多利羊(Sheep)为例,来说明上述这个问题,具体代码见下面: 多利羊(Sheep) publ ...

  7. rabbitMQ基于spring-rabbitnq

    一.什么是MQ MQ全称为Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递 ...

  8. React:List and key

    在React中,可以通过数组方法返回一组 组件元素,并将该数组作为render()的js插值. function NumberList(props) { const numbers = props.n ...

  9. python3.x 基础一:str字符串方法

    *字符串不能更改值 数据类型字符串str |  capitalize(...)   返回字符串中第一个字母大写 |      S.capitalize() -> str |       |    ...

  10. flatbuffer介绍和用法

    介绍 flatbuffer是google发布的一个跨平台序列化框架具有如下特点 1.对序列化的数据不需要打包和拆包 2.内存和效率速度高,扩展灵活 3.代码依赖较少 4.强类型设计,编译期即可完成类型 ...