Question

817. Linked List Components

Solution

题目大意:给一个链表和该链表元素组成的一个子数组,求子数组在链表中组成多少个片段,每个片段中可有多个连续的元素

思路:构造一个set用来存储子数组元素用于判断是否存在,遍历链表,如果当前元素不存在而下一个元素存在就表示一个片段的开始了,遍历前先判断首元素是否存在

Java实现:

public int numComponents(ListNode head, int[] G) {
Set<Integer> existSet = new HashSet<>();
for (int tmp : G) {
existSet.add(tmp);
}
int ans = existSet.contains(head.val) ? 1 : 0;
ListNode cur = head;
while (cur.next != null) {
if (!existSet.contains(cur.val) && existSet.contains(cur.next.val)) {
ans++;
}
cur = cur.next;
}
return ans;
}

817. Linked List Components - LeetCode的更多相关文章

  1. #Leetcode# 817. Linked List Components

    https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...

  2. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. (链表 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 ...

  4. LeetCode 817. Linked List Components (链表组件)

    题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...

  5. 817. Linked List Components

    1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...

  6. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  7. [Swift]LeetCode817. 链表组件 | Linked List Components

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  8. leetcode817 Linked List Components

    """ We are given head, the head node of a linked list containing unique integer value ...

  9. Reverse Linked List II [LeetCode]

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

随机推荐

  1. C语言常用字符串函数

    string.h头文件中常用的函数 C 库函数 - strcat() char *strcat(char *dest, const char *src) 把 src 所指向的字符串追加到 dest 所 ...

  2. Altium design使用日常故障总结

    1.altiumdesigner09如何将不同的板子拼在一起发给工厂?打开这两个图,其中一个图ctrl+a,ctrl+c,打开另一个图pastespecial.放置时选取一边对齐.制版时告诉厂家做个V ...

  3. PCB模块化布局系列之时钟电路设计(晶振、晶体)

    一.晶体在一个电路系统中, 时钟是必不可少的一部分.如人的心脏的作用,如果电路系统的时钟出错了,系统就会发生紊乱,因此在PCB 中设计,一个好的时钟电路是非常必要的.我们常用的时钟电路有:晶体.晶振. ...

  4. 谈谈关于CSS中transform属性之scale

    谈谈关于scale属性 scale是什么? 根据W3C定义 ,scale主要是进行缩放和转化: scale能做什么? 1.1px细线 <div class="wrap"> ...

  5. mpvue打包没有app.json等配置文件的解决方法

    问题 一早上折腾了1个小时,小程序始终提示查找不到'app.json'文件.mpvue重新打包,光生成内容文件无配置文件. 解决办法 出错原因:版本问题 只需要把packpage.json里的mpvu ...

  6. java中为什么把Checked Exception翻译成受检的异常?

    6.Checked Exception(受检的异常) 马克-to-win:为什么我大胆的把Checked Exception翻译成受检的异常?因为这类异常,编译器检查发现到它后会强令你catch它或t ...

  7. java中哪块代码或说什么代码应该放在try块中呢?

    我怎么知道哪块代码可能出现问题,从而放在try块儿中呢?马 克-to-win:一个笨办法,开始时,你并不加try,但你发现,运行时,用户赋给除数一个0,所以程序在这崩溃了,于是你就把这块代码加个try ...

  8. RestTemplate-HTTP工具

    RestTemplate 是由 Spring 提供的一个 HTTP 请求工具.在上文的案例中,开发者也可以不使用 RestTemplate ,使用 Java 自带的 HttpUrlConnection ...

  9. spring-基于注解的aop开发(快速入门)

    步骤: 1.导入坐标 <dependency> <groupId>junit</groupId> <artifactId>junit</artif ...

  10. NodeJs学习日报day8——接口编写

    今天看了黑马NodeJs中关于接口编写以及跨域问题的视频