Skip to content

Commit 23b2f70

Browse files
committed
feat: add solutions to lc problem: No.1791.Find Center of Star Graph
1 parent b902df8 commit 23b2f70

File tree

5 files changed

+90
-18
lines changed

5 files changed

+90
-18
lines changed

solution/1700-1799/1791.Find Center of Star Graph/README.md

+36-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
<li>题目数据给出的 <code>edges</code> 表示一个有效的星型图</li>
4141
</ul>
4242

43-
4443
## 解法
4544

4645
<!-- 这里可写通用的实现逻辑 -->
@@ -54,15 +53,23 @@
5453
```python
5554
class Solution:
5655
def findCenter(self, edges: List[List[int]]) -> int:
57-
return edges[0][0] if edges[0][0] == edges[1][0] or edges[0][0] == edges[1][1] else edges[0][1]
56+
a, b = edges[0]
57+
c, d = edges[1]
58+
return a if a == c or a == d else b
5859
```
5960

6061
### **Java**
6162

6263
<!-- 这里可写当前语言的特殊实现逻辑 -->
6364

6465
```java
65-
66+
class Solution {
67+
public int findCenter(int[][] edges) {
68+
int a = edges[0][0], b = edges[0][1];
69+
int c = edges[1][0], d = edges[1][1];
70+
return a == c || a == d ? a : b;
71+
}
72+
}
6673
```
6774

6875
### **TypeScript**
@@ -77,6 +84,32 @@ function findCenter(edges: number[][]): number {
7784
};
7885
```
7986

87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int findCenter(vector<vector<int>>& edges) {
93+
int a = edges[0][0], b = edges[0][1];
94+
int c = edges[1][0], d = edges[1][1];
95+
return a == c || a == d ? a : b;
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func findCenter(edges [][]int) int {
104+
a, b := edges[0][0], edges[0][1]
105+
c, d := edges[1][0], edges[1][1]
106+
if a == c || a == d {
107+
return a
108+
}
109+
return b
110+
}
111+
```
112+
80113
### **...**
81114

82115
```

solution/1700-1799/1791.Find Center of Star Graph/README_EN.md

+36-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
<p>There is an undirected <strong>star</strong> graph consisting of <code>n</code> nodes labeled from <code>1</code> to <code>n</code>. A star graph is a graph where there is one <strong>center</strong> node and <strong>exactly</strong> <code>n - 1</code> edges that connect the center node with every other node.</p>
88

9-
10-
119
<p>You are given a 2D integer array <code>edges</code> where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between the nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>. Return the center of the given star graph.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
@@ -28,12 +24,8 @@
2824

2925
</pre>
3026

31-
32-
3327
<p><strong>Example 2:</strong></p>
3428

35-
36-
3729
<pre>
3830

3931
<strong>Input:</strong> edges = [[1,2],[5,1],[1,3],[1,4]]
@@ -42,14 +34,10 @@
4234

4335
</pre>
4436

45-
46-
4737
<p>&nbsp;</p>
4838

4939
<p><strong>Constraints:</strong></p>
5040

51-
52-
5341
<ul>
5442
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
5543
<li><code>edges.length == n - 1</code></li>
@@ -68,13 +56,21 @@
6856
```python
6957
class Solution:
7058
def findCenter(self, edges: List[List[int]]) -> int:
71-
return edges[0][0] if edges[0][0] == edges[1][0] or edges[0][0] == edges[1][1] else edges[0][1]
59+
a, b = edges[0]
60+
c, d = edges[1]
61+
return a if a == c or a == d else b
7262
```
7363

7464
### **Java**
7565

7666
```java
77-
67+
class Solution {
68+
public int findCenter(int[][] edges) {
69+
int a = edges[0][0], b = edges[0][1];
70+
int c = edges[1][0], d = edges[1][1];
71+
return a == c || a == d ? a : b;
72+
}
73+
}
7874
```
7975

8076
### **TypeScript**
@@ -89,6 +85,32 @@ function findCenter(edges: number[][]): number {
8985
};
9086
```
9187

88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int findCenter(vector<vector<int>>& edges) {
94+
int a = edges[0][0], b = edges[0][1];
95+
int c = edges[1][0], d = edges[1][1];
96+
return a == c || a == d ? a : b;
97+
}
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func findCenter(edges [][]int) int {
105+
a, b := edges[0][0], edges[0][1]
106+
c, d := edges[1][0], edges[1][1]
107+
if a == c || a == d {
108+
return a
109+
}
110+
return b
111+
}
112+
```
113+
92114
### **...**
93115

94116
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func findCenter(edges [][]int) int {
2+
a, b := edges[0][0], edges[0][1]
3+
c, d := edges[1][0], edges[1][1]
4+
if a == c || a == d {
5+
return a
6+
}
7+
return b
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int findCenter(int[][] edges) {
3+
int a = edges[0][0], b = edges[0][1];
4+
int c = edges[1][0], d = edges[1][1];
5+
return a == c || a == d ? a : b;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class Solution:
22
def findCenter(self, edges: List[List[int]]) -> int:
3-
return edges[0][0] if edges[0][0] == edges[1][0] or edges[0][0] == edges[1][1] else edges[0][1]
3+
a, b = edges[0]
4+
c, d = edges[1]
5+
return a if a == c or a == d else b

0 commit comments

Comments
 (0)