20221012 - 817. Linked List Components 链表组件(哈希表)

61 阅读1分钟

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 <= 10e4
  • 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.

Solution

自己写的暴力算法:

  • 先把链表的数据存到一个新数组 check 内,然后根据给出的数组 nums 遍历 check ,把出现在 nums 中的元素在 check 中置为 -1
  • 遍历 check ,找到连续一个 -1 序列 ans++ ,中断重新开始找(寻找用 connect 变量辅助判断)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
int numComponents(struct ListNode* head, int* nums, int numsSize){
    int i, j, n, connect, ans, *check;
    struct ListNode* p;
    n = 0;
    p = head;
    while (p) {
        n++;
        p = p->next;
    }
    check = (int*)malloc(sizeof(int) * n);
    for (i = 0, p = head; i < n; i++, p = p->next) {
        check[i] = p->val;
    }
    for (i = 0; i < n; i++) {
        for (j = 0; j < numsSize; j++) {
            if (check[i] == nums[j]) {
                check[i] = -1;
                break;
            }
        }
    }
    connect = 0;
    ans = 0;
    for (i = 0; i < n; i++) {
        if (check[i] == -1) {
            connect = 1;
        } else {
            if (connect == 1) {
                connect = 0;
                ans++;
            }
        }
    }
    if (connect == 1) ans++;
    free(check);
    return ans;
}

题解的思路:

此题需要计算组件的个数,只需在链表中计算有多少组件的起始位置即可。当一个节点满足以下条件之一时,它是组件的起始位置:

  • 节点的值在数组 nums\textit{nums} 中且节点位于链表起始位置;
  • 节点的值在数组 nums\textit{nums} 中且节点的前一个点不在数组 nums\textit{nums} 中。

遍历链表,计算出满足条件的点的个数即可。因为需要多次判断值是否位于数组 nums\textit{nums} 中,用一个哈希集合保存数组 nums\textit{nums} 中的点可以降低时间复杂度。

按题解思路优化后:(还是慢)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool check(int val, int* nums, int numsSize)
{
    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == val) return true;
    }
    return false;
}

int numComponents(struct ListNode* head, int* nums, int numsSize){
    int i, ans;
    if (check(head->val, nums, numsSize)) ans = 1;
    else ans = 0;
    while (head->next) {
        if (check(head->next->val, nums, numsSize) && !check(head->val, nums, numsSize)) ans++;
        head = head->next;
    }
    return ans;
}

哈希优化:

以一个长度为数据范围的数组,用桶的方式存每个数据是否出现过。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
int numComponents(struct ListNode* head, int* nums, int numsSize){
    int i, ans, check[10000] = {0};
    for (i = 0; i < numsSize; i++) {
        check[nums[i]] = 1;
    }
    if (check[head->val]) ans = 1;
    else ans = 0;
    while (head->next) {
        if (check[head->next->val] && !check[head->val]) ans++;
        head = head->next;
    }
    return ans;
}

题目链接:817. 链表组件 - 力扣(LeetCode)