Skip to content

Commit 772acd2

Browse files
committed
Add solution #1791
1 parent 0e47c29 commit 772acd2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
1716|[Calculate Money in Leetcode Bank](./1716-calculate-money-in-leetcode-bank.js)|Easy|
258258
1748|[Sum of Unique Elements](./1748-sum-of-unique-elements.js)|Easy|
259259
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
260+
1791|[Find Center of Star Graph](./1791-find-center-of-star-graph.js)|Easy|
260261
1832|[Check if the Sentence Is Pangram](./1832-check-if-the-sentence-is-pangram.js)|Easy|
261262
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
262263
1886|[Determine Whether Matrix Can Be Obtained By Rotation](./1886-determine-whether-matrix-can-be-obtained-by-rotation.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 1791. Find Center of Star Graph
3+
* https://leetcode.com/problems/find-center-of-star-graph/
4+
* Difficulty: Easy
5+
*
6+
* There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a
7+
* graph where there is one center node and exactly n - 1 edges that connect the center node with
8+
* every other node.
9+
*
10+
* You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an
11+
* edge between the nodes ui and vi. Return the center of the given star graph.
12+
*/
13+
14+
/**
15+
* @param {number[][]} edges
16+
* @return {number}
17+
*/
18+
var findCenter = function(edges) {
19+
const set = new Set();
20+
for (const edge of edges) {
21+
for (const node of edge) {
22+
if (set.has(node)) {
23+
return node;
24+
}
25+
set.add(node);
26+
}
27+
}
28+
};

0 commit comments

Comments
 (0)