Skip to content

Commit

Permalink
update 133 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 2, 2023
1 parent 4abe53e commit 2cd6010
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,4 @@ void dfs(int[][] grid, int x, int y, boolean[][] visited, int ox, int oy, List<L
}
}

public static void main(String[] args) {
//System.out.println(true + false);
}
}
43 changes: 42 additions & 1 deletion leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,49 @@ public Node(int _val, ArrayList<Node> _neighbors) {

public class CloneGraph {

// TODO : fix below
// V0
// IDEA : DFS
public Node cloneGraph(Node node) {

// init hashmap
// NOTE : hashmap form : <Integer, Node>
HashMap<Integer, Node> _visited = new HashMap<>();
return _clone(_visited, node);
}

private Node _clone(HashMap<Integer, Node> visited, Node node){

// NOTE !!! handle edge case, if node is null, return itself directly
if (node == null) {
return node;
}

int cur_val = node.val;

// case 1) already visited, then return hashmap 's value (Node type) directly
if (visited.containsKey(cur_val)){
return visited.get(cur_val);
}

// NOTE !!! we init copied node as below
Node copiedNode = new Node(node.val, new ArrayList());
visited.put(cur_val, copiedNode);

// case 2) node is NOT visited yet, we go through all its neighbors,
for (Node _node : node.neighbors){
// NOTE !!! op here : we add new copied node to copiedNode.neighbors,
// (e.g. : copiedNode.neighbors.add)
// instead of return it directly
copiedNode.neighbors.add(_clone(visited, _node));
}

// NOTE !!! we return copiedNode as final step in code
// -> once code reach here, means all after all recursive call are completed
// -> copiedNode should have all collected nodes
return copiedNode;
}

// V0'
// Node clonedNode = new Node();
// public Node cloneGraph(Node node) {
//
Expand Down
41 changes: 41 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace1.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev;

import LeetCodeJava.DataStructure.ListNode;
import LeetCodeJava.DataStructure.Node;
import LeetCodeJava.DataStructure.TreeNode;

import java.sql.Array;
Expand Down Expand Up @@ -971,4 +972,44 @@ private boolean _is_island(char[][] grid, int x, int y, boolean[][] seen){
return true;
}

// https://leetcode.com/problems/clone-graph/
/**
*
* if input = [[2,4],[1,3],[2,4],[1,3]]
*
* then, _visited :
* {1 : Node(1, List(2, 4)), 2 : Node(List(1,3)) ...}
*
*/
public Node cloneGraph(Node node) {

// init hashmap
HashMap<Integer, Node> _visited = new HashMap<>();
return _clone(_visited, node);
}

private Node _clone(HashMap<Integer, Node> visited, Node node){

if (node == null) {
return node;
}


int cur_val = node.val;

if (visited.containsKey(cur_val)){
return visited.get(cur_val);
}

Node copiedNode = new Node(node.val, new ArrayList());
visited.put(cur_val, copiedNode);


for (Node _node : node.neighbors){
copiedNode.neighbors.add(_clone(visited, _node));
}

return copiedNode; // ?
}

}

0 comments on commit 2cd6010

Please sign in to comment.