Skip to content

feat: add typescript solution to lc problem: No.0133.Clone Graph #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions solution/0100-0199/0133.Clone Graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@

```

### **TypeScript**

```ts
/**
* Definition for Node.
* class Node {
* val: number
* neighbors: Node[]
* constructor(val?: number, neighbors?: Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.neighbors = (neighbors===undefined ? [] : neighbors)
* }
* }
*/

function cloneGraph(node: Node | null): Node | null {
if (node == null) return null;

const visited = new Map();
visited.set(node, new Node(node.val));
const queue = [node];
while (queue.length) {
const cur = queue.shift();
for (let neighbor of cur.neighbors || []) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
const newNeighbor = new Node(neighbor.val, []);
visited.set(neighbor, newNeighbor);
}
const newNode = visited.get(cur);
newNode.neighbors.push(visited.get(neighbor));
}
}
return visited.get(node);
};
```

### **...**

```
Expand Down
37 changes: 37 additions & 0 deletions solution/0100-0199/0133.Clone Graph/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,43 @@ class Node {

```

### **TypeScript**

```ts
/**
* Definition for Node.
* class Node {
* val: number
* neighbors: Node[]
* constructor(val?: number, neighbors?: Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.neighbors = (neighbors===undefined ? [] : neighbors)
* }
* }
*/

function cloneGraph(node: Node | null): Node | null {
if (node == null) return null;

const visited = new Map();
visited.set(node, new Node(node.val));
const queue = [node];
while (queue.length) {
const cur = queue.shift();
for (let neighbor of cur.neighbors || []) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
const newNeighbor = new Node(neighbor.val, []);
visited.set(neighbor, newNeighbor);
}
const newNode = visited.get(cur);
newNode.neighbors.push(visited.get(neighbor));
}
}
return visited.get(node);
};
```

### **...**

```
Expand Down
32 changes: 32 additions & 0 deletions solution/0100-0199/0133.Clone Graph/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for Node.
* class Node {
* val: number
* neighbors: Node[]
* constructor(val?: number, neighbors?: Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.neighbors = (neighbors===undefined ? [] : neighbors)
* }
* }
*/

function cloneGraph(node: Node | null): Node | null {
if (node == null) return null;

const visited = new Map();
visited.set(node, new Node(node.val));
const queue = [node];
while (queue.length) {
const cur = queue.shift();
for (let neighbor of cur.neighbors || []) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
const newNeighbor = new Node(neighbor.val, []);
visited.set(neighbor, newNeighbor);
}
const newNode = visited.get(cur);
newNode.neighbors.push(visited.get(neighbor));
}
}
return visited.get(node);
};