817. Linked List Components - LeetCode
Question
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的更多相关文章
- #Leetcode# 817. Linked List Components
https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked l ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- (链表 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 (链表组件)
题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 lin ...
- 817. Linked List Components
1. 原始题目 We are given head, the head node of a linked list containing unique integer values. We are a ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- [Swift]LeetCode817. 链表组件 | Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- leetcode817 Linked List Components
""" We are given head, the head node of a linked list containing unique integer value ...
- 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-> ...
随机推荐
- 移动端比1px还小的border
巧用border 在移动端 经常出现border,细边框但有的时候 产品大大1px甚至乎会觉得不够细那么要如何写出比1px还要小的border下面是代码 希望对大家有所帮助 .thinner-bord ...
- VueJs单页应用实现微信网页授权及微信分享功能
在实际开发中,无论是做PC端.WebApp端还是微信公众号等类型的项目的时候,或多或少都会涉及到微信相关的开发,最近公司项目要求实现微信网页授权,并获取微信用户基本信息的功能及微信分享的功能,现在总算 ...
- JS 用状态机的思想看Generator之基本语法篇
前言 最近学习了阮一峰老师的<ECMAScript 6 入门>里的Generator相关知识,以及<你不知道的JS>中卷的异步编程部分.同时在SegmentFault问答区看到 ...
- java中什么是内部类?它有什么用?如何使用?
什么是内部类?马克-to-win:一句话:类中还有类.里边的叫内部类, 外边的叫外层类.有什么用?1)像文件夹一样,文件放文件夹里更清晰,内部类放外层类中, 清晰.主要从编程序的逻辑角度出发,有用.比 ...
- PAT B1076 Wifi密码
题目描述: 下面是微博上流传的一张照片:"各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同 ...
- xml中的<![CDATA[]]>和转义字符
被<![CDATA[]]>这个标记所包含的内容将表示为纯文本,比如<![CDATA[<]]>表示文本内容"<". 此标记用于xml文档中,我们先 ...
- vue行内动态添加样式或者动态添加类名
还是记录一下吧(๑•ᴗ•๑) <li :style="{backgroundImage:`url(${item.pic})`}" @click="chooseVip ...
- Python入门-面向对象三大特性-封装
一.封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容. 所以,在使用面向对象的封装特性时,需要: 将内容封装到某处 从某处调用被封装的内容 第一步:将内容封装到某处 sel ...
- c语言实现双链表的基本操作—增删改查
//初始化 Node*InitList() { Node*head=(Node*)malloc(sizeof(Node)); if(NULL==head) { printf("内存分配失败! ...
- 帝国cms修改成https后后台登陆空白的解决办法
以下方法适用帝国cms7.5版本: 7.5版本已经有了http和https自动识别,但是因为一些疑难杂症的原因,自动识别判断的不准,后台登录也是空白, 我们可以打开e/config.php查找'htt ...