[LeetCode] Linked List Components 链表组件

 

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

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

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:app

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.

Note:spa

  • If N is the length of the linked list given by head1 <= N <= 10000.
  • The value of each node in the linked list will be in the range [0, N - 1].
  • 1 <= G.length <= 10000.
  • G is a subset of all values in the linked list.

 

这道题给了咱们一个链表,又给了咱们一个结点值数组,里面不必定包括了链表中全部的结点值。让咱们返回结点值数组中有多少个相连的组件,由于缺失的结点值会将原链表断开,实际上就是让咱们求有多少个相连的子链表,题目中给的例子很好的说明题意。这道题并不须要什么特别高深的技巧,难懂的算法,直接按题目的要求来找就能够了。首先,为了快速的在结点值数组中查找某个结点值是否存在,咱们能够将全部的结点值放到一个HashSet中,这样咱们就能在常数级的时间复杂度中查找。而后咱们就能够来遍历链表了,对于遍历到的每一个结点值,咱们只有两种状况,在或者不在HashSet中。不在HashSet中的状况比较好办,说明此时断开了,而在HashSet中的结点,有多是该连续子链表的起始点,或者是中间的某个点,而咱们的计数器对该子链表只能自增1,因此咱们须要想办法来hanlde这种状况。博主最早想到的办法是先处理不在HashSet中的结点,处理方法就是直接跳到下一个结点。那么对于在HashSet中的结点,咱们首先将计数器res自增1,而后再来个循环,将以后全部在集合中的结点都遍历完,这样才不会对同一个子链表屡次增长计数器,参见代码以下:code

 

解法一:component

class Solution {
public:
    int numComponents(ListNode* head, vector<int>& G) {
        int res = 0;
        unordered_set<int> nodeSet(G.begin(), G.end());
        while (head) {
            if (!nodeSet.count(head->val)) {
                head = head->next;
                continue;
            }
            ++res;
            while (head && nodeSet.count(head->val)) {
                head = head->next;
            }
        }
        return res;
    }
};

 

咱们能够稍稍修改代码,使其更加简洁,咱们在遍历的时候进行判断,若是当前结点在集合中,而且当前结点是尾结点或者下一个结点不在集合中的时候,咱们让计数器自增1,经过这种操做,咱们不会多加也不会漏加计数器,参见代码以下:htm

 

解法二:blog

class Solution {
public:
    int numComponents(ListNode* head, vector<int>& G) {
        int res = 0;
        unordered_set<int> nodeSet(G.begin(), G.end());
        while (head) {
            if (nodeSet.count(head->val) && (!head->next || !nodeSet.count(head->next->val))) {
                ++res;
            }
            head = head->next;
        }
        return res;
    }
};

 

参考资料:

https://leetcode.com/problems/linked-list-components/description/

https://leetcode.com/problems/linked-list-components/solution/

https://leetcode.com/problems/linked-list-components/discuss/123842/C++JavaPython-Easy-and-Concise-Solution-with-Explanation

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索