-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
57 lines (48 loc) · 1.29 KB
/
my_solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Node {
constructor(val) {
this.val = val;
this.neighbors = [];
}
}
const buildGraph = (adjList) => {
const nodes = {};
// Create all the nodes
for (let i = 0; i < adjList.length; i++) {
const val = i + 1;
nodes[val] = new Node(val);
}
// Connect the nodes
for (let i = 0; i < adjList.length; i++) {
const node = nodes[i + 1]; // Get the corresponding Node object
for (let j = 0; j < adjList[i].length; j++) {
const neighborVal = adjList[i][j];
const neighborNode = nodes[neighborVal]; // Get the neighbor Node object
node.neighbors.push(neighborNode);
}
}
// Return the reference to the first node (val = 1)
return nodes[1];
}
/**
* @param {number[][]} edges
* @return {number}
*/
var findCenter = function (edges) {
let visited = new Map();
for (let i = 0; i < edges.length; i++) {
for (let uv = 0; uv < edges[i].length; uv++) {
if (visited.has(edges[i][uv])) {
return edges[i][uv];
}
visited.set(edges[i][uv], true)
console.log(visited)
}
}
};
// let x = buildGraph([[1, 2], [2, 3], [4, 2]])
// let x = findCenter([[1, 2], [2, 3], [4, 2]])
// let x = findCenter([[1, 3], [2, 3]])
let x = findCenter([[1, 2], [5, 1], [1, 3], [1, 4]])
console.log("x")
console.log(x)
// cloneGraph(node);