diff --git a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md index 746e0fb06..dd9cc1289 100755 --- a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md +++ b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md @@ -1,28 +1,26 @@ # [1791.Find Center of Star Graph][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. + +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. + +**Example 1:** -**Example 1:** +![1](./star_graph.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: edges = [[1,2],[2,3],[4,2]] +Output: 2 +Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Find Center of Star Graph -```go ``` - +Input: edges = [[1,2],[5,1],[1,3],[1,4]] +Output: 1 +``` ## 结语 diff --git a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution.go b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution.go index d115ccf5e..a9ebfa13b 100644 --- a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution.go +++ b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution.go @@ -1,5 +1,12 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(edges [][]int) int { + a, b := edges[0], edges[1] + if a[0] == b[1] || a[0] == b[0] { + return a[0] + } + if a[1] == b[1] || a[1] == b[0] { + return a[1] + } + return -1 } diff --git a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution_test.go b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution_test.go index 14ff50eb4..ef7a2be84 100644 --- a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution_test.go +++ b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{{1, 2}, {2, 3}, {4, 2}}, 2}, + {"TestCase2", [][]int{{1, 2}, {5, 1}, {1, 3}, {1, 4}}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/star_graph.png b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/star_graph.png new file mode 100644 index 000000000..392cf7c42 Binary files /dev/null and b/leetcode/1701-1800/1791.Find-Center-of-Star-Graph/star_graph.png differ