Skip to content

Commit 34545e8

Browse files
committed
modify code on class
1 parent 01cce63 commit 34545e8

10 files changed

+38
-45
lines changed

src/class10/Code01_BFS.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
public class Code01_BFS {
88

99
// 从node出发,进行宽度优先遍历
10-
public static void bfs(Node node) {
11-
if (node == null) {
10+
public static void bfs(Node start) {
11+
if (start == null) {
1212
return;
1313
}
1414
Queue<Node> queue = new LinkedList<>();
1515
HashSet<Node> set = new HashSet<>();
16-
queue.add(node);
17-
set.add(node);
16+
queue.add(start);
17+
set.add(start);
1818
while (!queue.isEmpty()) {
1919
Node cur = queue.poll();
2020
System.out.println(cur.value);

src/class10/Code02_DFS.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ public static void dfs(Node node) {
2727
}
2828
}
2929
}
30+
31+
32+
3033

3134
}

src/class10/Code03_TopologicalOrderDFS2.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ public static ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode>
5454
return ans;
5555
}
5656

57+
// 当前来到cur点,请返回cur点所到之处,所有的点次!
58+
// 返回(cur,点次)
59+
// 缓存!!!!!order
60+
// key : 某一个点的点次,之前算过了!
61+
// value : 点次是多少
5762
public static Record f(DirectedGraphNode cur, HashMap<DirectedGraphNode, Record> order) {
5863
if (order.containsKey(cur)) {
5964
return order.get(cur);
6065
}
66+
// cur的点次之前没算过!
6167
long nodes = 0;
6268
for (DirectedGraphNode next : cur.neighbors) {
6369
nodes += f(next, order).nodes;

src/class10/Code03_TopologySort.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class Code03_TopologySort {
1010

1111
// directed graph and no loop
1212
public static List<Node> sortedTopology(Graph graph) {
13+
// key 某个节点 value 剩余的入度
1314
HashMap<Node, Integer> inMap = new HashMap<>();
15+
// 只有剩余入度为0的点,才进入这个队列
1416
Queue<Node> zeroInQueue = new LinkedList<>();
1517
for (Node node : graph.nodes.values()) {
1618
inMap.put(node, node.in);

src/class10/Code04_Kruskal.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public int compare(Edge o1, Edge o2) {
8383
public static Set<Edge> kruskalMST(Graph graph) {
8484
UnionFind unionFind = new UnionFind();
8585
unionFind.makeSets(graph.nodes.values());
86+
// 从小的边到大的边,依次弹出,小根堆!
8687
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(new EdgeComparator());
8788
for (Edge edge : graph.edges) { // M 条边
8889
priorityQueue.add(edge); // O(logM)

src/class10/Code05_Prim.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public static Set<Edge> primMST(Graph graph) {
2323

2424
// 哪些点被解锁出来了
2525
HashSet<Node> nodeSet = new HashSet<>();
26+
27+
28+
2629
Set<Edge> result = new HashSet<>(); // 依次挑选的的边在result里
30+
2731
for (Node node : graph.nodes.values()) { // 随便挑了一个点
2832
// node 是开始点
2933
if (!nodeSet.contains(node)) {

src/class10/Code06_Dijkstra.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ public class Code06_Dijkstra {
1010
public static HashMap<Node, Integer> dijkstra1(Node from) {
1111
HashMap<Node, Integer> distanceMap = new HashMap<>();
1212
distanceMap.put(from, 0);
13+
// 打过对号的点
1314
HashSet<Node> selectedNodes = new HashSet<>();
1415
Node minNode = getMinDistanceAndUnselectedNode(distanceMap, selectedNodes);
1516
while (minNode != null) {
17+
// 原始点 -> minNode(跳转点) 最小距离distance
1618
int distance = distanceMap.get(minNode);
1719
for (Edge edge : minNode.edges) {
1820
Node toNode = edge.to;
1921
if (!distanceMap.containsKey(toNode)) {
2022
distanceMap.put(toNode, distance + edge.weight);
21-
} else {
23+
} else { // toNode
2224
distanceMap.put(edge.to, Math.min(distanceMap.get(toNode), distance + edge.weight));
2325
}
2426
}

src/class10/GraphGenerator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ public class GraphGenerator {
55
// matrix 所有的边
66
// N*3 的矩阵
77
// [weight, from节点上面的值,to节点上面的值]
8-
public static Graph createGraph(Integer[][] matrix) {
8+
//
9+
// [ 5 , 0 , 7]
10+
// [ 3 , 0, 1]
11+
//
12+
public static Graph createGraph(int[][] matrix) {
913
Graph graph = new Graph();
10-
for (int i = 0; i < matrix.length; i++) {
11-
// matrix[0][0], matrix[0][1] matrix[0][2]
12-
Integer weight = matrix[i][0];
13-
Integer from = matrix[i][1];
14-
Integer to = matrix[i][2];
14+
for (int i = 0; i < matrix.length; i++) {
15+
// 拿到每一条边, matrix[i]
16+
int weight = matrix[i][0];
17+
int from = matrix[i][1];
18+
int to = matrix[i][2];
1519
if (!graph.nodes.containsKey(from)) {
1620
graph.nodes.put(from, new Node(from));
1721
}

src/class10/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.ArrayList;
44

5-
// 点结构的描述 A 0
5+
// 点结构的描述
66
public class Node {
77
public int value;
88
public int in;

src/class11/Code06_ConvertToLetterString.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,9 @@ public static int process(char[] str, int i) {
3434
return res;
3535
}
3636
return process(str, i + 1);
37-
}
38-
39-
public static int dpWays2(String s) {
40-
if (s == null || s.length() == 0) {
41-
return 0;
42-
}
43-
char[] str = s.toCharArray();
44-
int N = str.length;
45-
int[] dp = new int[N+1];
46-
dp[N] = 1;
47-
for(int i = N-1; i >= 0; i--) {
48-
if (str[i] == '0') {
49-
dp[i] = 0;
50-
}
51-
if (str[i] == '1') {
52-
dp[i] = dp[i + 1];
53-
if (i + 1 < str.length) {
54-
dp[i] += dp[i + 2];
55-
}
56-
}
57-
if (str[i] == '2') {
58-
dp[i] = dp[i + 1];
59-
if (i + 1 < str.length && (str[i + 1] >= '0' && str[i + 1] <= '6')) {
60-
dp[i] += dp[i + 2]; // (i和i+1)作为单独的部分,后续有多少种方法
61-
}
62-
}
63-
}
64-
return dp[0];
65-
}
66-
37+
}
6738

68-
public static int dpWays(String s) {
39+
public static int dp(String s) {
6940
if (s == null || s.length() == 0) {
7041
return 0;
7142
}
@@ -94,8 +65,8 @@ public static int dpWays(String s) {
9465
}
9566

9667
public static void main(String[] args) {
97-
System.out.println(number("11111"));
98-
System.out.println(dpWays2("11111"));
68+
System.out.println(number("2132082"));
69+
System.out.println(dp("2132082"));
9970
}
10071

10172
}

0 commit comments

Comments
 (0)