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. 第 4 章 ROS运行管理

    第 4 章 ROS运行管理 ROS是多进程(节点)的分布式框架,一个完整的ROS系统实现: 可能包含多台主机:每台主机上又有多个工作空间(workspace):每个的工作空间中又包含多个功能包(pac ...

  2. SVG Sprite 入门(SVG图标解决方案)

    关于浏览器图标解决方案,一直就有很多 CSS Sprite,Icon Font,CSS Icon以及SVG.相对而言svg矢量图标以及支持浏览器自身解析的优点,很多团队都已经在使用了.这篇文章主要说明 ...

  3. HTML5 Canvas学习之路(六)

    一个炫酷的计时器 在慕课网看到一个canvas的课,感觉很炫酷,就把它看完了,然后记下来.http://www.imooc.com/learn/133 第一步:绘制要显示的时间 拿小球来绘制具体的数字 ...

  4. Canvas 制作海报

      HTML <template> <view class="content"> <view class="flex_row_c_c mod ...

  5. Androd点击一个选框取消其他选框

    说明: 我做的体温填报系统需要在填报体温时勾选有无特殊情况 当勾选'无'时需要将用户勾选的其他选框取消 当勾选其他选框时需要将'无'这个选框取消 效果: 代码: addTem.xml <Line ...

  6. 数据库number(4,3)表示什么

    1 你看 number(4,3)是表示 这个数 一共有4位是有效位,后面的3 表示有3个是小数也就是这个数 只能是1.234,这样格式的 最大只能是9.999,2 number(3,4) 表示这个数 ...

  7. EMS查看及修改邮箱发送和接受邮件大小的方法

    默认情况下,新建用户邮箱没有进行单独设置,故用户邮箱默认值为"Unlimited"(未限制),即遵从全局设置(继承邮箱数据库策略).通过EMS查看用户邮箱发送和接受邮件大小的默认值 ...

  8. rem,px,em最大的区别;

    px:px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.移动端的分辨率很多.所以px不适用移动端em:em的值不固定:其长度继承父级元素的字体大小rem:相对于根元素htm ...

  9. ELK日志保留7天-索引生命周期策略

    一.简介 ELK日志我们一般都是按天存储,例如索引名为"kafkalog-2022-04-05",因为日志量所占的存储是非常大的,我们不能一直保存,而是要定期清理旧的,这里就以保留 ...

  10. mosquitto使用与常用配置

    为了方便演示,我这里就用windows环境下安装的mosquitto进行操作,操作方式和linux系统下是一样的. 一.windows安装mosquitto 下载mosquitto mosquitto ...