★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-nrrnsrox-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We are given head
, the head node of a linked list containing unique integer values.node
We are also given the list G
, a subset of the values in the linked list.git
Return the number of connected components in G
, where two values are connected if they appear consecutively in the linked list.github
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
N
is the length of the linked list given by head
, 1 <= N <= 10000
. [0, N - 1]
.1 <= G.length <= 10000
.G
is a subset of all values in the linked list.給定一個鏈表(鏈表結點包含一個整型值)的頭結點 head
。code
同時給定列表 G
,該列表是上述鏈表中整型值的一個子集。component
返回列表 G
中組件的個數,這裏對組件的定義爲:鏈表中一段最長連續結點的值(該值必須在列表 G
中)構成的集合。htm
示例 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 }