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. Spark离线日志分析,连接Spark出现报错

    首先,我的代码是这样的 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} object ...

  2. Android 开发技术周报 Issue#279

    新闻 丧心病狂or形势所迫?谈谈Android奇葩的"链式启动" 传闻称Android TV将更名为Google TV 谷歌官宣Android 11 Beta发布会:6月3日见 教 ...

  3. python语法学习第十一天--迭代器

    迭代:类似循环,这一次的值作为下一次迭代的开始值 BIF:iter():将某个可以作为迭代器的容器变为迭代器   next():做下一次迭代 当next()到最后一个时,抛出StopIteration ...

  4. Spring Cloud 系列之 Bus 消息总线

    什么是消息总线 消息代理中间件构建一个共用的消息主题让所有微服务实例订阅,当该消息主题产生消息时会被所有微服务实例监听和消费. 消息代理又是什么?消息代理是一个消息验证.传输.路由的架构模式,主要用来 ...

  5. XLNet看这篇文章就足以!

    文章链接:https://arxiv.org/pdf/1906.08237.pdf 代码链接:英文--https://github.com/zihangdai/xlnet      中文--https ...

  6. Unity Singleton 单例类(Unity3D开发)

    一.添加单例模板类 using UnityEngine; public class Singleton<T> : MonoBehaviour where T : MonoBehaviour ...

  7. Python一切皆是对象,但这和内存管理有什么关系?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python的第15篇文章,我们来聊聊Python中内存管理机制,以及循环引用的问题. Python的内存管理机制 对于工程师而言,内 ...

  8. linux ,mac连接, git pull error, chmod修改文件的权限/chown修改文件和目录的所有者

    去项目目录下 启动服务 setsid npm start & Mac下如何用SSH连接远程Linux服务器 https://www.cnblogs.com/littleBit/p/536280 ...

  9. 新概念英语三 新东方主讲Lesson1

    新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...

  10. sklearn学习:为什么roc_auc_score()和auc()有不同的结果?

    为什么roc_auc_score()和auc()有不同的结果? auc():计算ROC曲线下的面积.即图中的area roc_auc_score():计算AUC的值,即输出的AUC 最佳答案 AUC并 ...