Skip to content

Commit a504532

Browse files
authored
Merge pull request halfrost#229 from gostool/leetcode1791
Leetcode1791
2 parents 2c04339 + f9adc70 commit a504532

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package leetcode
2+
3+
func findCenter(edges [][]int) int {
4+
if edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] {
5+
return edges[0][0]
6+
}
7+
return edges[0][1]
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1791 struct {
9+
para1791
10+
ans1791
11+
}
12+
13+
// para 是参数
14+
type para1791 struct {
15+
edges [][]int
16+
}
17+
18+
// ans 是答案
19+
type ans1791 struct {
20+
ans int
21+
}
22+
23+
func Test_Problem1791(t *testing.T) {
24+
25+
qs := []question1791{
26+
27+
{
28+
para1791{[][]int{{1, 2}, {2, 3}, {4, 2}}},
29+
ans1791{2},
30+
},
31+
32+
{
33+
para1791{[][]int{{1, 2}, {5, 1}, {1, 3}, {1, 4}}},
34+
ans1791{1},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 1791------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans1791, q.para1791
42+
fmt.Printf("【input】:%v ", p.edges)
43+
fmt.Printf("【output】:%v \n", findCenter(p.edges))
44+
}
45+
fmt.Printf("\n\n\n")
46+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# [1791.Find Center of Star Graph](https://leetcode-cn.com/problems/find-center-of-star-graph/)
2+
3+
## 题目
4+
5+
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.
6+
7+
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.
8+
9+
**Example 1:**
10+
11+
![https://assets.leetcode.com/uploads/2021/02/24/star_graph.png:w](https://assets.leetcode.com/uploads/2021/02/24/star_graph.png)
12+
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.
16+
17+
**Example 2:**
18+
19+
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
20+
Output: 1
21+
22+
**Constraints:**
23+
24+
- 3 <= n <= 100000
25+
- edges.length == n - 1
26+
- edges[i].length == 2
27+
- 1 <= ui, vi <= n
28+
- ui != vi
29+
- The given edges represent a valid star graph.
30+
31+
## 题目大意
32+
33+
有一个无向的 星型 图,由 n 个编号从 1 到 n 的节点组成。星型图有一个 中心 节点,并且恰有 n - 1 条边将中心节点与其他每个节点连接起来。
34+
35+
给你一个二维整数数组 edges ,其中 edges[i] = [ui, vi] 表示在节点 ui 和 vi 之间存在一条边。请你找出并返回 edges 所表示星型图的中心节点。
36+
37+
## 解题思路
38+
39+
- 求出edges中前两个元素的共同值,即是中心节点
40+
41+
## 代码
42+
43+
```go
44+
package leetcode
45+
46+
func findCenter(edges [][]int) int {
47+
if edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1] {
48+
return edges[0][0]
49+
}
50+
return edges[0][1]
51+
}
52+
```

0 commit comments

Comments
 (0)