在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ We are given We are also given the list Return the number of connected components in 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:
给定一个链表(链表结点包含一个整型值)的头结点 同时给定列表 返回列表 示例 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。 注意:
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 } Runtime: 156 ms
Memory Usage: 19.5 MB
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 } Runtime: 160 ms
Memory Usage: 19.8 MB
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 }
|
请发表评论