leetcode 817. 链表组件

给定链表头结点 head,该链表上的每个结点都有一个 唯一的整型值 。

同时给定列表 G,该列表是上述链表中整型值的一个子集。

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

 

示例 1:

输入:
head: 0->1->2->3
G = [0, 1, 3]
输出: 2
解释:
链表中,0 和 1 是相连接的,且 G 中不包含 2,所以 [0, 1] 是 G 的一个组件,同理 [3] 也是一个组件,故返回 2。
示例 2:

输入:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
输出: 2
解释:
链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。
 

提示:

如果 N 是给定链表 head 的长度,1 <= N <= 10000。
链表中每个结点的值所在范围为 [0, N - 1]。
1 <= G.length <= 10000
G 是链表中所有结点的值的一个子集.

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

1:采用map来记录数组中有哪些数字。

2:遍历链表,若链表中的数字在map中出现,则说明存在,是一个组件中的元素.

3:直到链表中的数字在map中没有的时候,表示一个组件结束。计数器 + 1.

4:按照上述逻辑,遍历链表到结束。

    public int numComponents(ListNode head, int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>(nums.length << 1);
        for (int item : nums) {
            map.put(item, 1);
        }
        int res = 0;

        while (head != null) {
            if (map.get(head.val) != null) {
                while (head != null && map.get(head.val) != null) {
                    head = head.next;
                }
                res++;
            } else {
                head = head.next;
            }
        }
        return res;
    }

leetcode 817. 链表组件

leetcode 817. 链表组件

上一篇:Vue 源码分析——总领


下一篇:链表+数学:实现两个大数相加 ( Leetcode 02/67/371/445/989 )