Skip to content

Commit 44f1a48

Browse files
committed
Add solution and test-cases for problem 1791
1 parent 1940a25 commit 44f1a48

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# [1791.Find Center of Star Graph][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There is an undirected **star** graph consisting of `n` nodes labeled from `1` to `n`. A star graph is a graph where there is one **center** node and **exactly** `n - 1` edges that connect the center node with every other node.
5+
6+
You are given a 2D integer array `edges` where each `edges[i] = [ui, vi]` indicates that there is an edge between the nodes `ui` and `vi`. Return the center of the given star graph.
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![1](./star_graph.png)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: edges = [[1,2],[2,3],[4,2]]
14+
Output: 2
15+
Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find Center of Star Graph
23-
```go
2420
```
25-
21+
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
22+
Output: 1
23+
```
2624

2725
## 结语
2826

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(edges [][]int) int {
4+
a, b := edges[0], edges[1]
5+
if a[0] == b[1] || a[0] == b[0] {
6+
return a[0]
7+
}
8+
if a[1] == b[1] || a[1] == b[0] {
9+
return a[1]
10+
}
11+
return -1
512
}

leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 2}, {2, 3}, {4, 2}}, 2},
17+
{"TestCase2", [][]int{{1, 2}, {5, 1}, {1, 3}, {1, 4}}, 1},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}
Loading

0 commit comments

Comments
 (0)