diff --git a/src/class10/Code01_BFS.java b/src/class10/Code01_BFS.java index c219524..e6d7fc2 100644 --- a/src/class10/Code01_BFS.java +++ b/src/class10/Code01_BFS.java @@ -7,14 +7,14 @@ import java.util.Queue; public class Code01_BFS { // 从node出发,进行宽度优先遍历 - public static void bfs(Node node) { - if (node == null) { + public static void bfs(Node start) { + if (start == null) { return; } Queue queue = new LinkedList<>(); HashSet set = new HashSet<>(); - queue.add(node); - set.add(node); + queue.add(start); + set.add(start); while (!queue.isEmpty()) { Node cur = queue.poll(); System.out.println(cur.value); diff --git a/src/class10/Code02_DFS.java b/src/class10/Code02_DFS.java index 6ecc3cf..4c73d6b 100644 --- a/src/class10/Code02_DFS.java +++ b/src/class10/Code02_DFS.java @@ -27,5 +27,8 @@ public class Code02_DFS { } } } + + + } diff --git a/src/class10/Code03_TopologicalOrderDFS2.java b/src/class10/Code03_TopologicalOrderDFS2.java index 7cf5f79..c74c1ab 100644 --- a/src/class10/Code03_TopologicalOrderDFS2.java +++ b/src/class10/Code03_TopologicalOrderDFS2.java @@ -54,10 +54,16 @@ public class Code03_TopologicalOrderDFS2 { return ans; } + // 当前来到cur点,请返回cur点所到之处,所有的点次! + // 返回(cur,点次) + // 缓存!!!!!order + // key : 某一个点的点次,之前算过了! + // value : 点次是多少 public static Record f(DirectedGraphNode cur, HashMap order) { if (order.containsKey(cur)) { return order.get(cur); } + // cur的点次之前没算过! long nodes = 0; for (DirectedGraphNode next : cur.neighbors) { nodes += f(next, order).nodes; diff --git a/src/class10/Code03_TopologySort.java b/src/class10/Code03_TopologySort.java index 2c79e2f..431c67d 100644 --- a/src/class10/Code03_TopologySort.java +++ b/src/class10/Code03_TopologySort.java @@ -10,7 +10,9 @@ public class Code03_TopologySort { // directed graph and no loop public static List sortedTopology(Graph graph) { + // key 某个节点 value 剩余的入度 HashMap inMap = new HashMap<>(); + // 只有剩余入度为0的点,才进入这个队列 Queue zeroInQueue = new LinkedList<>(); for (Node node : graph.nodes.values()) { inMap.put(node, node.in); diff --git a/src/class10/Code04_Kruskal.java b/src/class10/Code04_Kruskal.java index e95a1fc..eb1342f 100644 --- a/src/class10/Code04_Kruskal.java +++ b/src/class10/Code04_Kruskal.java @@ -83,6 +83,7 @@ public class Code04_Kruskal { public static Set kruskalMST(Graph graph) { UnionFind unionFind = new UnionFind(); unionFind.makeSets(graph.nodes.values()); + // 从小的边到大的边,依次弹出,小根堆! PriorityQueue priorityQueue = new PriorityQueue<>(new EdgeComparator()); for (Edge edge : graph.edges) { // M 条边 priorityQueue.add(edge); // O(logM) diff --git a/src/class10/Code05_Prim.java b/src/class10/Code05_Prim.java index 9564d3b..a937115 100644 --- a/src/class10/Code05_Prim.java +++ b/src/class10/Code05_Prim.java @@ -23,7 +23,11 @@ public class Code05_Prim { // 哪些点被解锁出来了 HashSet nodeSet = new HashSet<>(); + + + Set result = new HashSet<>(); // 依次挑选的的边在result里 + for (Node node : graph.nodes.values()) { // 随便挑了一个点 // node 是开始点 if (!nodeSet.contains(node)) { diff --git a/src/class10/Code06_Dijkstra.java b/src/class10/Code06_Dijkstra.java index b20d01d..713cd7d 100644 --- a/src/class10/Code06_Dijkstra.java +++ b/src/class10/Code06_Dijkstra.java @@ -10,15 +10,17 @@ public class Code06_Dijkstra { public static HashMap dijkstra1(Node from) { HashMap distanceMap = new HashMap<>(); distanceMap.put(from, 0); + // 打过对号的点 HashSet selectedNodes = new HashSet<>(); Node minNode = getMinDistanceAndUnselectedNode(distanceMap, selectedNodes); while (minNode != null) { + // 原始点 -> minNode(跳转点) 最小距离distance int distance = distanceMap.get(minNode); for (Edge edge : minNode.edges) { Node toNode = edge.to; if (!distanceMap.containsKey(toNode)) { distanceMap.put(toNode, distance + edge.weight); - } else { + } else { // toNode distanceMap.put(edge.to, Math.min(distanceMap.get(toNode), distance + edge.weight)); } } diff --git a/src/class10/GraphGenerator.java b/src/class10/GraphGenerator.java index 1180e20..73287f6 100644 --- a/src/class10/GraphGenerator.java +++ b/src/class10/GraphGenerator.java @@ -5,13 +5,17 @@ public class GraphGenerator { // matrix 所有的边 // N*3 的矩阵 // [weight, from节点上面的值,to节点上面的值] - public static Graph createGraph(Integer[][] matrix) { + // + // [ 5 , 0 , 7] + // [ 3 , 0, 1] + // + public static Graph createGraph(int[][] matrix) { Graph graph = new Graph(); - for (int i = 0; i < matrix.length; i++) { - // matrix[0][0], matrix[0][1] matrix[0][2] - Integer weight = matrix[i][0]; - Integer from = matrix[i][1]; - Integer to = matrix[i][2]; + for (int i = 0; i < matrix.length; i++) { + // 拿到每一条边, matrix[i] + int weight = matrix[i][0]; + int from = matrix[i][1]; + int to = matrix[i][2]; if (!graph.nodes.containsKey(from)) { graph.nodes.put(from, new Node(from)); } diff --git a/src/class10/Node.java b/src/class10/Node.java index d022d12..256ab4c 100644 --- a/src/class10/Node.java +++ b/src/class10/Node.java @@ -2,7 +2,7 @@ package class10; import java.util.ArrayList; -// 点结构的描述 A 0 +// 点结构的描述 public class Node { public int value; public int in; diff --git a/src/class11/Code06_ConvertToLetterString.java b/src/class11/Code06_ConvertToLetterString.java index aa1c7ec..1458cf4 100644 --- a/src/class11/Code06_ConvertToLetterString.java +++ b/src/class11/Code06_ConvertToLetterString.java @@ -34,38 +34,9 @@ public class Code06_ConvertToLetterString { return res; } return process(str, i + 1); - } - - public static int dpWays2(String s) { - if (s == null || s.length() == 0) { - return 0; - } - char[] str = s.toCharArray(); - int N = str.length; - int[] dp = new int[N+1]; - dp[N] = 1; - for(int i = N-1; i >= 0; i--) { - if (str[i] == '0') { - dp[i] = 0; - } - if (str[i] == '1') { - dp[i] = dp[i + 1]; - if (i + 1 < str.length) { - dp[i] += dp[i + 2]; - } - } - if (str[i] == '2') { - dp[i] = dp[i + 1]; - if (i + 1 < str.length && (str[i + 1] >= '0' && str[i + 1] <= '6')) { - dp[i] += dp[i + 2]; // (i和i+1)作为单独的部分,后续有多少种方法 - } - } - } - return dp[0]; - } - + } - public static int dpWays(String s) { + public static int dp(String s) { if (s == null || s.length() == 0) { return 0; } @@ -94,8 +65,8 @@ public class Code06_ConvertToLetterString { } public static void main(String[] args) { - System.out.println(number("11111")); - System.out.println(dpWays2("11111")); + System.out.println(number("2132082")); + System.out.println(dp("2132082")); } }