817. Linked List Components - Medium

We are given head, the head node of a linked list containing unique integer values.

We are also given the list G, a subset of the values in the linked list.

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

Example 1:

Input:
head: 0->1->2->3
G = [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
G = [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.

先把G中的元素加入set

遍历linked list,如果当前指针指向节点的值在set中存在,并且下一个为空/下一个不存在,说明是一个独立的component,component数+1;

如果下一个不为空并且下一个值在set中存在,说明这仍然是同一个component,指针指向下一个元素

time: O(n), space: O(k)  -- k: length of G

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int numComponents(ListNode head, int[] G) {
        Set<Integer> set = new HashSet<>();
        for(int g : G) {
            set.add(g);
        }

        int res = 0;
        while(head != null) {
            if(set.contains(head.val) && (head.next == null || !set.contains(head.next.val))) {
                res++;
            }
            head = head.next;
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10185134.html

时间: 2024-07-30 18:58:42

817. Linked List Components - Medium的相关文章

817. Linked List Components

We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components in G, where two values are connected if they appear

#Leetcode# 817. Linked List Components

https://leetcode.com/problems/linked-list-components/ We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected componen

LeetCode 817. Linked List Components (链表组件)

题目标签:Linked List 题目给了我们一组 linked list, 和一组 G, 让我们找到 G 在 linked list 里有多少组相连的部分. 把G 存入 hashset,遍历 linked list, 利用 hashset 来检查目前的点 和 下一个点 是否在G 里面. 如果目前的点在G里面,下一个点不在,说明这里断开了.具体看code. Java Solution: Runtime:  7 ms, faster than 81 % Memory Usage: 40 MB, l

leetcode817 Linked List Components

1 """ 2 We are given head, the head node of a linked list containing unique integer values. 3 4 We are also given the list G, a subset of the values in the linked list. 5 6 Return the number of connected components in G, where two values ar

92. Reverse Linked List II - Medium

Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 只反转从m到n的部分链表 M1: iterative 首先找到prev的位置(开始反转位置的前一个节

114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】

Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Accepted 218,918 Submissions 533,947 [解析]由上图可知,如果右子树不为空,则右子树最后肯定为左子树最有一个靠右的孩子节点的右子树,而

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

EBT 道客巴巴的加密与破解 - 实用组合工具箱

本文系列共有三篇,前两篇链接在此: < EBT 道客巴巴的加密与破解 -免费下载器的基础> <EBT 道客巴巴的加密与破解 序章> 写到这里有点忧伤,有两点,一是CSDN在删除我之前上传的附件,二是doc88.com服务器端没有完成破解,也就是说我只解开了doc88客户端.不想说什么,纯贴个代码就走. AIR 项目主文件: <?xml version="1.0" encoding="utf-8"?> <s:WindowedA