★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10565310.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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.
Note:
- If
N
is the length of the linked list given byhead
,1 <= 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.
给定一个链表(链表结点包含一个整型值)的头结点 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
是链表中所有结点的值的一个子集.
148ms
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func numComponents(_ head: ListNode?, _ G: [Int]) -> Int { 14 15 let numbers = Set(G) 16 var node = head 17 var count = 0 18 var isSameComponent = false 19 while let current = node { 20 if numbers.contains(current.val) { 21 if !isSameComponent { 22 isSameComponent = true 23 count += 1 24 } 25 } else { 26 isSameComponent = false 27 } 28 node = node?.next 29 } 30 31 return count 32 } 33 }
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func numComponents(_ head: ListNode?, _ G: [Int]) -> Int { 14 guard head != nil && G.count > 0 else { 15 return 0 16 } 17 let setG: Set = Set(G) 18 19 var result = 1 20 var pre = head 21 var l1 = head?.next 22 while l1 != nil { 23 var flag = false 24 if setG.contains(pre!.val) && !setG.contains(l1!.val){ 25 var l2 = l1?.next 26 while l2 != nil{ 27 if setG.contains(l2!.val){ 28 pre = l2 29 l1 = l2?.next 30 result += 1 31 flag = true 32 break 33 } 34 l2 = l2?.next 35 } 36 } 37 if !flag{ 38 pre = l1 39 l1 = l1?.next 40 } 41 } 42 return result 43 } 44 }
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * public var val: Int 5 * public var next: ListNode? 6 * public init(_ val: Int) { 7 * self.val = val 8 * self.next = nil 9 * } 10 * } 11 */ 12 class Solution { 13 func numComponents(_ head: ListNode?, _ G: [Int]) -> Int { 14 var head = head 15 var res:Int = 0 16 var nodeSet:Set<Int> = Set<Int>(G) 17 while (head != nil) 18 { 19 if nodeSet.contains(head!.val) && (head?.next == nil || !nodeSet.contains(head!.next!.val)) 20 { 21 res += 1 22 } 23 head = head?.next 24 } 25 return res 26 } 27 }
176ms
1 class Solution { 2 func numComponents(_ head: ListNode?, _ g: [Int]) -> Int { 3 let gSet = Set(g) 4 5 var count = 0 6 var connected = false 7 var possibleNode = head 8 9 while let node = possibleNode { 10 switch (connected, gSet.contains(node.val)) { 11 case (true, false): 12 connected = false 13 case (false, true): 14 connected = true 15 count += 1 16 default: 17 ( /* Do nothing. */ ) 18 } 19 possibleNode = node.next 20 } 21 22 return count 23 } 24 }
184ms
1 class Solution { 2 func numComponents(_ head: ListNode?, _ G: [Int]) -> Int { 3 var count = 0 4 var set = Set(G) 5 var node = head 6 var hasCut = false 7 while node != nil { 8 if !set.contains(node!.val) { 9 hasCut = true 10 } else { 11 if hasCut { 12 count += 1 13 hasCut = false 14 } 15 if count == 0 { count = 1 } 16 } 17 node = node?.next 18 } 19 return count 20 } 21 }