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. 10本 Linux PDF 书籍免费分享

    本篇文章主要分享以下Linux开发PDF书籍 一.Linux程序设计二.Unix环境高级编程三.Unix_Linux编程实践教程四.鸟哥的私房菜五.深入理解Linux内核六.Linux命令行与shel ...

  2. 一个未知宽高的元素在div中垂直水平居中

    <body> <div id="#div1"> <img src="img1.png"></img> </ ...

  3. 学习笔记 - Sass的安装与使用手册

    最近因为工作需要,自学了Sass.现在将学习笔记整理在这里,供大家参考. 1. Sass的安装 Sass的编辑器安装方法有很多,大致能分为两种:应用程序(application)和命令行界面(comm ...

  4. java中throws子句是怎么用的?工作原理是什么

    7.throws子句 马克-to-win:当你的方法里抛出了checked异常,如你不catch,代表你当时不处理(不想处理或没条件处理),但你必须得通过"throws那个异常"告 ...

  5. .NET程序设计实验四

    实验四  文件操作 一.实验目的 1. 掌握窗口控件的使用方法: 2. 掌握文件系统的操作方法.File 类和 Directory类的使用. 二.实验要求 根据要求,编写 C#程序,并将程序代码和运行 ...

  6. 鸿蒙JS 开发整理

    目录 一.前言: 二.鸿蒙 JS UI框架 2.1 JS UI特性 2.2 架构 2.3 新的UI框架结构 三.API 四.最后 一.前言: 5月25日,华为对外宣布计划在6月2日正式举办鸿蒙手机发布 ...

  7. 数据库篇:mysql事务原理之MVCC视图+锁

    前言 数据库的事务特性 数据并发读写时遇到的一致性问题 mysql事务的隔离级别 MVCC的实现原理 锁和隔离级别 关注公众号,一起交流,微信搜一搜: 潜行前行 1 数据库的事务特性 原子性:同一个事 ...

  8. 从数据库中获取图片编号,然后通过request获取图片下载

    import pandas as pd from pandas.core.dtypes.dtypes import register_extension_dtype from sqlalchemy i ...

  9. Vue实战-购物车案例

    Vue实战-购物车案例 普通购物车 实现的功能:添加商品到购物车,计算总价 <!DOCTYPE html> <html lang="en"> <hea ...

  10. 基于pgrouting的路径规划处理

    对于GIS业务来说,路径规划是非常基础的一个业务,一般公司如果处理,都会直接选择调用已经成熟的第三方的接口,比如高德.百度等.当然其实路径规划的算法非常多,像比较著名的Dijkstra.A*算法等.当 ...