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, less than 93 %

完成日期:05/14/2019

关键点:HashSet

/**
 * 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<>();
        int result = 0;

        for(int num : G)
        {
            set.add(num);
        }

        for(ListNode node = head; node != null; node = node.next)
        {
            if(set.contains(node.val)
               && (node.next == null || !set.contains(node.next.val)))
                result++;
        }

        return result;
    }
}

参考资料:Le‘e‘tCode Discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/11186769.html

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

LeetCode 817. Linked List Components (链表组件)的相关文章

#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 141. Linked List Cycle 链表

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 题目大意: 判断一个单链表是否存在环. 思路: 采用快慢指针来处理. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int va

[LeetCode] Reverse Linked List 倒置链表

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 之前做到Reverse Linked List II 倒置链表之二的时候我还纳闷怎么只有二没有一呢,原来真是忘了啊,现在才加上,这道题跟之前那道比起来简单不少,题目为了增加些许难度,让我们分别用

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

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

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

Leetcode:Linked List Cycle 链表是否存在环

Linked List Cycle: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解题分析: 大致思想就是设置两个指针,一个指针每次走两步,一个指针每次走一步,如果这两个指针碰头了,那么一定就存在环 可以类比两个人在环形操场跑步,同时出发,一个跑得快,一个跑得慢,如果跑得快的人追上跑得慢的人,那么跑得快的人相当于多跑了一整

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl