modify code on class

pull/6/head
左程云 5 years ago
parent 01cce63534
commit 34545e8ff2

@ -7,14 +7,14 @@ import java.util.Queue;
public class Code01_BFS { public class Code01_BFS {
// 从node出发进行宽度优先遍历 // 从node出发进行宽度优先遍历
public static void bfs(Node node) { public static void bfs(Node start) {
if (node == null) { if (start == null) {
return; return;
} }
Queue<Node> queue = new LinkedList<>(); Queue<Node> queue = new LinkedList<>();
HashSet<Node> set = new HashSet<>(); HashSet<Node> set = new HashSet<>();
queue.add(node); queue.add(start);
set.add(node); set.add(start);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Node cur = queue.poll(); Node cur = queue.poll();
System.out.println(cur.value); System.out.println(cur.value);

@ -27,5 +27,8 @@ public class Code02_DFS {
} }
} }
} }
} }

@ -54,10 +54,16 @@ public class Code03_TopologicalOrderDFS2 {
return ans; return ans;
} }
// 当前来到cur点请返回cur点所到之处所有的点次
// 返回cur点次
// 缓存order
// key : 某一个点的点次,之前算过了!
// value : 点次是多少
public static Record f(DirectedGraphNode cur, HashMap<DirectedGraphNode, Record> order) { public static Record f(DirectedGraphNode cur, HashMap<DirectedGraphNode, Record> order) {
if (order.containsKey(cur)) { if (order.containsKey(cur)) {
return order.get(cur); return order.get(cur);
} }
// cur的点次之前没算过
long nodes = 0; long nodes = 0;
for (DirectedGraphNode next : cur.neighbors) { for (DirectedGraphNode next : cur.neighbors) {
nodes += f(next, order).nodes; nodes += f(next, order).nodes;

@ -10,7 +10,9 @@ public class Code03_TopologySort {
// directed graph and no loop // directed graph and no loop
public static List<Node> sortedTopology(Graph graph) { public static List<Node> sortedTopology(Graph graph) {
// key 某个节点 value 剩余的入度
HashMap<Node, Integer> inMap = new HashMap<>(); HashMap<Node, Integer> inMap = new HashMap<>();
// 只有剩余入度为0的点才进入这个队列
Queue<Node> zeroInQueue = new LinkedList<>(); Queue<Node> zeroInQueue = new LinkedList<>();
for (Node node : graph.nodes.values()) { for (Node node : graph.nodes.values()) {
inMap.put(node, node.in); inMap.put(node, node.in);

@ -83,6 +83,7 @@ public class Code04_Kruskal {
public static Set<Edge> kruskalMST(Graph graph) { public static Set<Edge> kruskalMST(Graph graph) {
UnionFind unionFind = new UnionFind(); UnionFind unionFind = new UnionFind();
unionFind.makeSets(graph.nodes.values()); unionFind.makeSets(graph.nodes.values());
// 从小的边到大的边,依次弹出,小根堆!
PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(new EdgeComparator()); PriorityQueue<Edge> priorityQueue = new PriorityQueue<>(new EdgeComparator());
for (Edge edge : graph.edges) { // M 条边 for (Edge edge : graph.edges) { // M 条边
priorityQueue.add(edge); // O(logM) priorityQueue.add(edge); // O(logM)

@ -23,7 +23,11 @@ public class Code05_Prim {
// 哪些点被解锁出来了 // 哪些点被解锁出来了
HashSet<Node> nodeSet = new HashSet<>(); HashSet<Node> nodeSet = new HashSet<>();
Set<Edge> result = new HashSet<>(); // 依次挑选的的边在result里 Set<Edge> result = new HashSet<>(); // 依次挑选的的边在result里
for (Node node : graph.nodes.values()) { // 随便挑了一个点 for (Node node : graph.nodes.values()) { // 随便挑了一个点
// node 是开始点 // node 是开始点
if (!nodeSet.contains(node)) { if (!nodeSet.contains(node)) {

@ -10,15 +10,17 @@ public class Code06_Dijkstra {
public static HashMap<Node, Integer> dijkstra1(Node from) { public static HashMap<Node, Integer> dijkstra1(Node from) {
HashMap<Node, Integer> distanceMap = new HashMap<>(); HashMap<Node, Integer> distanceMap = new HashMap<>();
distanceMap.put(from, 0); distanceMap.put(from, 0);
// 打过对号的点
HashSet<Node> selectedNodes = new HashSet<>(); HashSet<Node> selectedNodes = new HashSet<>();
Node minNode = getMinDistanceAndUnselectedNode(distanceMap, selectedNodes); Node minNode = getMinDistanceAndUnselectedNode(distanceMap, selectedNodes);
while (minNode != null) { while (minNode != null) {
// 原始点 -> minNode(跳转点) 最小距离distance
int distance = distanceMap.get(minNode); int distance = distanceMap.get(minNode);
for (Edge edge : minNode.edges) { for (Edge edge : minNode.edges) {
Node toNode = edge.to; Node toNode = edge.to;
if (!distanceMap.containsKey(toNode)) { if (!distanceMap.containsKey(toNode)) {
distanceMap.put(toNode, distance + edge.weight); distanceMap.put(toNode, distance + edge.weight);
} else { } else { // toNode
distanceMap.put(edge.to, Math.min(distanceMap.get(toNode), distance + edge.weight)); distanceMap.put(edge.to, Math.min(distanceMap.get(toNode), distance + edge.weight));
} }
} }

@ -5,13 +5,17 @@ public class GraphGenerator {
// matrix 所有的边 // matrix 所有的边
// N*3 的矩阵 // N*3 的矩阵
// [weight, from节点上面的值to节点上面的值] // [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(); Graph graph = new Graph();
for (int i = 0; i < matrix.length; i++) { for (int i = 0; i < matrix.length; i++) {
// matrix[0][0], matrix[0][1] matrix[0][2] // 拿到每一条边, matrix[i]
Integer weight = matrix[i][0]; int weight = matrix[i][0];
Integer from = matrix[i][1]; int from = matrix[i][1];
Integer to = matrix[i][2]; int to = matrix[i][2];
if (!graph.nodes.containsKey(from)) { if (!graph.nodes.containsKey(from)) {
graph.nodes.put(from, new Node(from)); graph.nodes.put(from, new Node(from));
} }

@ -2,7 +2,7 @@ package class10;
import java.util.ArrayList; import java.util.ArrayList;
// 点结构的描述 A 0 // 点结构的描述
public class Node { public class Node {
public int value; public int value;
public int in; public int in;

@ -34,38 +34,9 @@ public class Code06_ConvertToLetterString {
return res; return res;
} }
return process(str, i + 1); 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) { if (s == null || s.length() == 0) {
return 0; return 0;
} }
@ -94,8 +65,8 @@ public class Code06_ConvertToLetterString {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(number("11111")); System.out.println(number("2132082"));
System.out.println(dpWays2("11111")); System.out.println(dp("2132082"));
} }
} }

Loading…
Cancel
Save