首页 > 其他分享 >[LeetCode] 817. Linked List Components

[LeetCode] 817. Linked List Components

时间:2022-10-16 06:44:05浏览次数:69  
标签:ListNode cur val nums List LeetCode 链表 next Linked

You are given the head of a linked list containing unique integer values and an integer array nums that is a subset of the linked list values.

Return the number of connected components in nums where two values are connected if they appear consecutively in the linked list.

Example 1:

Input: head = [0,1,2,3], nums = [0,1,3]
Output: 2
Explanation: 0 and 1 are connected, so [0, 1] and [3] are the two connected components.

Example 2:

Input: head = [0,1,2,3,4], nums = [0,3,1,4]
Output: 2
Explanation: 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components. 

Constraints:

  • The number of nodes in the linked list is n.
  • 1 <= n <= 104
  • 0 <= Node.val < n
  • All the values Node.val are unique.
  • 1 <= nums.length <= n
  • 0 <= nums[i] < n
  • All the values of nums are unique.

链表组件。

给定链表头结点 head,该链表上的每个结点都有一个 唯一的整型值 。同时给定列表 nums,该列表是上述链表中整型值的一个子集。

返回列表 nums 中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 nums 中)构成的集合。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/linked-list-components
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道链表题,我直接讲思路。因为 input 给的数组元素都是 unique 的,所以我用一个 hashset 记录所有出现过的元素。接着我再从链表的头结点开始遍历整个链表。对于当前节点 cur,如果他存在于 hashset,那么起码他就是某个链表组件的一部分,count 就要++。接着往后看,如果 cur 的下一个节点 cur.next 也存在于 hashset,那么说明 cur 和 cur.next 都是当前这个组件的一部分,count保持不变。如果中间有断开的情形,那么当我们再遇到另一个存在于 hashset 的节点的时候,就说明找到了一个新的链表组件。

时间O(n)

空间O(n)

Java实现

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode() {}
 7  *     ListNode(int val) { this.val = val; }
 8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 9  * }
10  */
11 class Solution {
12     public int numComponents(ListNode head, int[] nums) {
13         HashSet<Integer> set = new HashSet<>();
14         for (int num : nums) {
15             set.add(num);
16         }
17         
18         ListNode cur = head;
19         int count = 0;
20         while (cur != null) {
21             if (set.contains(cur.val)) {
22                 count++;
23                 while (cur != null && set.contains(cur.val)) {
24                     cur = cur.next;
25                 }
26             } else {
27                 cur = cur.next;
28             }
29         }
30         return count;
31     }
32 }

 

LeetCode 题目总结

标签:ListNode,cur,val,nums,List,LeetCode,链表,next,Linked
From: https://www.cnblogs.com/cnoodle/p/16795577.html

相关文章

  • [LeetCode] 2095. Delete the Middle Node of a Linked List
    Youaregiventhe head ofalinkedlist. Delete the middlenode,andreturn the head ofthemodifiedlinkedlist.The middlenode ofalinkedlistof......
  • Leetcode简单题背后的数学规律 | LCP 11. 期望个数统计
    最近签到打卡,每日额外再刷两道题攒积分。遇到一个简单题LCP11.期望个数统计,挺有意思的,记录一下分析过程并重温概率学知识。题目给定n个数的数组scores,小A和小B负责......
  • 【LeetCode-769. medium】最多能完成排序的块
    ​​力扣​​ 解题报告:注意这种【根据一个要求,将数组分成多个区间】类模型的问题(比如汽车加油站、加法表达式求和),套路就这三步:1、初始化2、for循环或者while,里面三步  ......
  • 面试官:ArrayList扩容机制,你了解吗?
    最近耗时5个月整理的10w字Java面试手册,涵盖了Java面试几乎都会问的面试题目,直达链接​​10w字Java面试手册​​小熊学Java在线地址:https://javaxiaobear.gitee.io/1、底层......
  • LinkedHashSet集合特点
    packagepackage8;importjava.util.LinkedHashSet;/*LinkedHashSet集合特点哈希表和链表实现的Set接口,具有可预测的迭代次序由链表保证元素有序,也就是说元素的存储......
  • 集合—AyyayList
    集合和数组相比较:数组是定长的,类型是不变的,可以存储基本类型。集合是变长的,类型是可变的,不能存储基本类型。集合的三种接口:通用的父类:CollectionList:ArrayListSet:Has......
  • 刷题 LeetCode 24. 两两交换链表中的节点 19.删除链表的倒数第N个节点 面试题 02.07.
    代码随想录LeetCode24. 两两交换链表中的节点carl链表#dummyNode#双指针#递归思路借助dummyNode简化判断条件使用双指针更清晰一些,两个指针分别指向要交换的两......
  • python学习:获取指定目录下所有文件名os.walk和os.listdir
    1.os.walk返回指定路径下所有文件和子文件夹中所有文件列表其中文件夹下路径如下:importosdeffile_name_walk(file_dir):forroot,dirs,filesinos.walk(f......
  • List集合存储学生对象用三种方式遍历
    packagepackage5;importpackage4.Student;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;//List集合存储学生对象用三种方式遍......
  • 练习题06List
    分析以下需求,并用代码实现:(1)有如下代码:(2)定义方法统计集合中指定元素出现的次数,如"a"3,"b"2,"c"1List<String>list=newArrayList<>();list.add("a");list.......