Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit af6c63f

Browse files
aQuaaQua
aQua
authored and
aQua
committed
817 finish
1 parent 6bb8f38 commit af6c63f

File tree

4 files changed

+205
-13
lines changed

4 files changed

+205
-13
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [817. Linked List Components](https://leetcode.com/problems/linked-list-components/)
2+
3+
## 题目
4+
5+
We are given head, the head node of a linked list containing unique integer values.
6+
7+
We are also given the list G, a subset of the values in the linked list.
8+
9+
Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list.
10+
11+
Example 1:
12+
13+
```text
14+
Input:
15+
head: 0->1->2->3
16+
G = [0, 1, 3]
17+
Output: 2
18+
Explanation:
19+
0 and 1 are connected, so [0, 1] and [3] are the two connected components.
20+
```
21+
22+
Example 2:
23+
24+
```text
25+
Input:
26+
head: 0->1->2->3->4
27+
G = [0, 3, 1, 4]
28+
Output: 2
29+
Explanation:
30+
0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
31+
```
32+
33+
Note:
34+
35+
1. If N is the length of the linked list given by head, 1 <= N <= 10000.
36+
1. The value of each node in the linked list will be in the range [0, N - 1].
37+
1. 1 <= G.length <= 10000.
38+
1. G is a subset of all values in the linked list.
39+
40+
## 解题思路
41+
42+
见程序注释
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package problem0817
2+
3+
import (
4+
"sort"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
)
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* type ListNode struct {
12+
* Val int
13+
* Next *ListNode
14+
* return 0
15+
}
16+
*/
17+
18+
// ListNode is from kit
19+
type ListNode = kit.ListNode
20+
21+
func numComponents(head *ListNode, G []int) int {
22+
// 收集 list 中各个数的索引号
23+
// 方便以后判断连通性
24+
indexs := make(map[int]int, len(G)*2)
25+
i := 0
26+
for head != nil {
27+
indexs[head.Val] = i
28+
i++
29+
head = head.Next
30+
}
31+
32+
// 初步统计连通性
33+
// temp[i][0] == 3 表示,第 i 个联通的链条 起点 是 3
34+
// temp[i][1] == 5 表示,第 i 个联通的链条 终点 是 5
35+
temp := make([][2]int, 0, len(G))
36+
for _, num := range G {
37+
idx := indexs[num]
38+
isConnected := false
39+
for i := range temp {
40+
// 如果 idx 与 temp[i] 的 起点 相连
41+
if idx+1 == temp[i][0] {
42+
temp[i][0] = idx
43+
isConnected = true
44+
break
45+
}
46+
// 如果 idx 与 temp[i] 的 终点 相连
47+
if temp[i][1] == idx-1 {
48+
temp[i][1] = idx
49+
isConnected = true
50+
break
51+
}
52+
}
53+
// 如果 idx 不与 temp 中的任何链条相连
54+
if !isConnected {
55+
temp = append(temp, [2]int{idx, idx})
56+
}
57+
}
58+
59+
// 按照链条的起点排序
60+
sort.Slice(temp, func(i int, j int) bool {
61+
return temp[i][0] < temp[j][0]
62+
})
63+
64+
t := temp[0]
65+
res := 1
66+
for i := 1; i < len(temp); i++ {
67+
// 如果前后两个链条不相连
68+
if t[1]+1 != temp[i][0] {
69+
res++
70+
}
71+
t = temp[i]
72+
}
73+
74+
return res
75+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package problem0817
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/aQuaYi/LeetCode-in-Go/kit"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// tcs is testcase slice
13+
var tcs = []struct {
14+
head []int
15+
G []int
16+
ans int
17+
}{
18+
19+
{
20+
[]int{0, 2, 4, 3, 1},
21+
[]int{3, 2, 4},
22+
1,
23+
},
24+
25+
{
26+
[]int{1, 2, 0, 4, 3},
27+
[]int{3, 4, 0, 2, 1},
28+
1,
29+
},
30+
31+
{
32+
[]int{0, 1, 2},
33+
[]int{1, 0, 2},
34+
1,
35+
},
36+
37+
{
38+
[]int{0, 1, 2},
39+
[]int{1, 0},
40+
1,
41+
},
42+
43+
{
44+
[]int{0, 1, 2, 3},
45+
[]int{0, 1, 3},
46+
2,
47+
},
48+
49+
{
50+
[]int{0, 1, 2, 3, 4},
51+
[]int{0, 3, 1, 4},
52+
2,
53+
},
54+
55+
// 可以有多个 testcase
56+
}
57+
58+
func Test_numComponents(t *testing.T) {
59+
ast := assert.New(t)
60+
61+
for _, tc := range tcs {
62+
fmt.Printf("~~%v~~\n", tc)
63+
head := kit.Ints2List(tc.head)
64+
ast.Equal(tc.ans, numComponents(head, tc.G), "输入:%v", tc)
65+
}
66+
}
67+
68+
func Benchmark_numComponents(b *testing.B) {
69+
for i := 0; i < b.N; i++ {
70+
for _, tc := range tcs {
71+
head := kit.Ints2List(tc.head)
72+
numComponents(head, tc.G)
73+
}
74+
}
75+
}

leetcode.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 1146,
4-
"Updated": "2018-05-06T19:33:05.564465389+08:00",
4+
"Updated": "2018-05-07T10:44:31.443065238+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 166,
@@ -2089,7 +2089,7 @@
20892089
"ID": 172,
20902090
"Title": "Factorial Trailing Zeroes",
20912091
"TitleSlug": "factorial-trailing-zeroes",
2092-
"PassRate": "36%",
2092+
"PassRate": "37%",
20932093
"Difficulty": "Easy",
20942094
"IsAccepted": true,
20952095
"IsPaid": false,
@@ -3901,7 +3901,7 @@
39013901
"ID": 323,
39023902
"Title": "Number of Connected Components in an Undirected Graph",
39033903
"TitleSlug": "number-of-connected-components-in-an-undirected-graph",
3904-
"PassRate": "48%",
3904+
"PassRate": "49%",
39053905
"Difficulty": "Medium",
39063906
"IsAccepted": false,
39073907
"IsPaid": true,
@@ -6421,7 +6421,7 @@
64216421
"ID": 533,
64226422
"Title": "Lonely Pixel II",
64236423
"TitleSlug": "lonely-pixel-ii",
6424-
"PassRate": "45%",
6424+
"PassRate": "44%",
64256425
"Difficulty": "Medium",
64266426
"IsAccepted": false,
64276427
"IsPaid": true,
@@ -8233,7 +8233,7 @@
82338233
"ID": 684,
82348234
"Title": "Redundant Connection",
82358235
"TitleSlug": "redundant-connection",
8236-
"PassRate": "44%",
8236+
"PassRate": "43%",
82378237
"Difficulty": "Medium",
82388238
"IsAccepted": true,
82398239
"IsPaid": false,
@@ -9001,7 +9001,7 @@
90019001
"ID": 748,
90029002
"Title": "Shortest Completing Word",
90039003
"TitleSlug": "shortest-completing-word",
9004-
"PassRate": "52%",
9004+
"PassRate": "51%",
90059005
"Difficulty": "Medium",
90069006
"IsAccepted": true,
90079007
"IsPaid": false,
@@ -9037,7 +9037,7 @@
90379037
"ID": 751,
90389038
"Title": "IP to CIDR",
90399039
"TitleSlug": "ip-to-cidr",
9040-
"PassRate": "55%",
9040+
"PassRate": "54%",
90419041
"Difficulty": "Easy",
90429042
"IsAccepted": false,
90439043
"IsPaid": true,
@@ -9565,7 +9565,7 @@
95659565
"ID": 795,
95669566
"Title": "Number of Subarrays with Bounded Maximum",
95679567
"TitleSlug": "number-of-subarrays-with-bounded-maximum",
9568-
"PassRate": "40%",
9568+
"PassRate": "41%",
95699569
"Difficulty": "Medium",
95709570
"IsAccepted": true,
95719571
"IsPaid": false,
@@ -9637,7 +9637,7 @@
96379637
"ID": 801,
96389638
"Title": "Minimum Swaps To Make Sequences Increasing",
96399639
"TitleSlug": "minimum-swaps-to-make-sequences-increasing",
9640-
"PassRate": "25%",
9640+
"PassRate": "26%",
96419641
"Difficulty": "Medium",
96429642
"IsAccepted": true,
96439643
"IsPaid": false,
@@ -9925,7 +9925,7 @@
99259925
"ID": 825,
99269926
"Title": "Friends Of Appropriate Ages",
99279927
"TitleSlug": "friends-of-appropriate-ages",
9928-
"PassRate": "24%",
9928+
"PassRate": "25%",
99299929
"Difficulty": "Medium",
99309930
"IsAccepted": false,
99319931
"IsPaid": false,
@@ -9961,7 +9961,7 @@
99619961
"ID": 828,
99629962
"Title": "Unique Letter String",
99639963
"TitleSlug": "unique-letter-string",
9964-
"PassRate": "24%",
9964+
"PassRate": "28%",
99659965
"Difficulty": "Hard",
99669966
"IsAccepted": false,
99679967
"IsPaid": false,
@@ -9973,7 +9973,7 @@
99739973
"ID": 829,
99749974
"Title": "Consecutive Numbers Sum",
99759975
"TitleSlug": "consecutive-numbers-sum",
9976-
"PassRate": "22%",
9976+
"PassRate": "23%",
99779977
"Difficulty": "Medium",
99789978
"IsAccepted": false,
99799979
"IsPaid": false,
@@ -9997,7 +9997,7 @@
99979997
"ID": 831,
99989998
"Title": "Masking Personal Information",
99999999
"TitleSlug": "masking-personal-information",
10000-
"PassRate": "42%",
10000+
"PassRate": "43%",
1000110001
"Difficulty": "Medium",
1000210002
"IsAccepted": false,
1000310003
"IsPaid": false,

0 commit comments

Comments
 (0)