-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path817-LinkedListComponents.go
More file actions
119 lines (108 loc) · 3.23 KB
/
817-LinkedListComponents.go
File metadata and controls
119 lines (108 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
// 817. Linked List Components
// 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:
// 0 -> 1 -> 2 -> 3
// <img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg" />
// 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:
// 0 -> 1 -> 2 -> 3 -> 4
// <img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg" />
// 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 <= 10^4
// 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.
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
// 打印链表
func printListNode(l *ListNode) {
if nil == l {
return
}
for {
if nil == l.Next {
fmt.Print(l.Val)
break
} else {
fmt.Print(l.Val, " -> ")
}
l = l.Next
}
fmt.Println()
}
// 数组创建链表
func makeListNode(arr []int) *ListNode {
if (len(arr) == 0) {
return nil
}
var l = (len(arr) - 1)
var head = &ListNode{arr[l], nil}
for i := l - 1; i >= 0; i-- {
var n = &ListNode{arr[i], head}
head = n
}
return head
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func numComponents(head *ListNode, nums []int) int {
targets := make(map[int]bool)
for _, v := range nums {
targets[v] = true
}
res := 0
var prev *ListNode
for cur := head; cur != nil; cur = cur.Next {
if prev != nil {
if !targets[prev.Val] && targets[cur.Val] {
res++
}
} else {
if targets[cur.Val] {
res++
}
}
prev = cur
}
return res
}
func main() {
// Example 1:
// 0 -> 1 -> 2 -> 3
// <img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg" />
// 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.
list1 := makeListNode([]int{0,1,2,3})
printListNode(list1)
fmt.Println(numComponents(list1,[]int{0,1,3})) // 2
// Example 2:
// 0 -> 1 -> 2 -> 3 -> 4
// <img src="https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg" />
// 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.
list2 := makeListNode([]int{0,1,2,3,4})
printListNode(list2)
fmt.Println(numComponents(list2,[]int{0,3,1,4})) // 2
}