Skip to content

Commit

Permalink
Add solution and test-cases for problem 1791
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jun 27, 2024
1 parent 1940a25 commit 44f1a48
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
28 changes: 13 additions & 15 deletions leetcode/1701-1800/1791.Find-Center-of-Star-Graph/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
11 changes: 9 additions & 2 deletions leetcode/1701-1800/1791.Find-Center-of-Star-Graph/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 44f1a48

Please sign in to comment.