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-> ...
随机推荐
- 玩转 React(四)- 创造一个新的 HTML 标签
在第二篇文章 <新型前端开发方式> 中有说到 React 有很爽的一点就是给我们一种创造 HTML 标签的能力,那么今天这篇文章就详细讲解下 React 是如何提供这种能力的,作为前端开发 ...
- 有关placeholder在ie9中的一点折腾
有关placeholder在ie9中的一点折腾. placeholder属性定义: placeholder 属性规定可描述输入字段预期值的简短的提示信息(比如:一个样本值或者预期格式的短描述). 问题 ...
- 从ES6重新认识JavaScript设计模式(三): 建造者模式
1 什么是建造者模式? 建造者模式(Builder)是将一个复杂对象的构建层与其表示层相互分离,同样的构建过程可采用不同的表示. 建造者模式的特点是分步构建一个复杂的对象,可以用不同组合或顺序建造出不 ...
- Linux 0.11源码阅读笔记-文件IO流程
文件IO流程 用户进程read.write在高速缓冲块上读写数据,高速缓冲块和块设备交换数据. 什么时机将磁盘块数据读到缓冲块? 什么时机将缓冲块数据刷到磁盘块? 函数调用关系 read/write( ...
- JavaScript 小技巧 数组去重
const array = [1, 2, 3, 3, 5, 5, 1]; const uniqueArray = [...new Set(array)]; console.log(uniqueArra ...
- JS中Map和ForEach的区别
定义 forEach()方法: 针对每一个元素执行提供的函数. map()方法: 创建一个新的数组,其中每一个元素由调用数组中的每一个元素执行提供的函数得来. 区别 forEach()方法不会返回执行 ...
- 在ios里面返回上一级报错问题
$("#backPrev").attr("href","javascript:void(0);").click(function(){ ...
- 阿里云申请SSL证书 并部署到SpringBoot项目
前提 有一台阿里云的服务器(安装了java环境) 有已经备案的域名,并且域名绑定上面的服务器 申请SSL证书 申请教程:https://blog.csdn.net/yunweifun/article/ ...
- Spring Boot-场景启动器
分析上文快速入门 1.查看pom文件导入的依赖(starter的父项目) <parent> <artifactId>spring-boot-starter-parent< ...
- Python 速通爆肝、列表推导式、生成器、装饰器、生命游戏
列表推导式.赋值.切片(替换.插入).字符串处理与判断.enumerate().格式化字符串.读写文件.global 关键字.字符串startswith().类与对象.生成器.装饰器.Self.*ar ...