更新 LCR155.java、LeetCode207.java、LeetCode210.java 和 LeetCode583.java 文件,进行代码优化和逻辑调整。
This commit is contained in:
parent
1e4aa82952
commit
a75259b346
@ -1,4 +1,4 @@
|
||||
package cn.whaifree.leetCode.LinkedList;
|
||||
package cn.whaifree.LCR;
|
||||
|
||||
import org.junit.Test;
|
||||
|
100
src/main/java/cn/whaifree/leetCode/Graph/LeetCode207.java
Normal file
100
src/main/java/cn/whaifree/leetCode/Graph/LeetCode207.java
Normal file
@ -0,0 +1,100 @@
|
||||
package cn.whaifree.leetCode.Graph;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/16 0:02
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode207 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
int numCourses = 4;
|
||||
int[][] prerequisites = {{1, 0},{0,2},{1,3}};
|
||||
System.out.println(new Solution().canFinish(numCourses, prerequisites));
|
||||
}
|
||||
class Solution {
|
||||
public boolean canFinish(int numCourses, int[][] prerequisites) {
|
||||
List<List<Integer>> graph = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
graph.add(new ArrayList<>());
|
||||
}
|
||||
|
||||
// 统计入度个数
|
||||
int[] inGre = new int[numCourses];
|
||||
for (int i = 0; i < prerequisites.length; i++) {
|
||||
int course = prerequisites[i][0];
|
||||
int preCourse = prerequisites[i][1];
|
||||
graph.get(preCourse).add(course);
|
||||
inGre[course]++;
|
||||
}
|
||||
|
||||
// 对所有入度为0的进入队列
|
||||
Deque<Integer> queue = new ArrayDeque<>();
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if (inGre[i] == 0) {
|
||||
queue.add(i);
|
||||
}
|
||||
}
|
||||
// 出对,并去边
|
||||
int exeCount = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
Integer pop = queue.pop();
|
||||
exeCount++;
|
||||
// 遍历这个pop点的出边
|
||||
List<Integer> popOut = graph.get(pop);
|
||||
for (int i = 0; i < popOut.size(); i++) {
|
||||
int deleteSideNode = popOut.get(i);
|
||||
inGre[deleteSideNode]--;
|
||||
if (inGre[deleteSideNode] == 0) {
|
||||
queue.add(deleteSideNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果队列中没有元素了,但还有边,返回false
|
||||
return exeCount == numCourses;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Solution1 {
|
||||
public boolean canFinish(int numCourses, int[][] prerequisites) {
|
||||
// 统计入度个数
|
||||
int[] map = new int[2000];
|
||||
for (int i = 0; i < prerequisites.length; i++) {
|
||||
map[prerequisites[i][0]]++;
|
||||
}
|
||||
// 对所有入度为0的进入队列
|
||||
Deque<Integer> queue = new ArrayDeque<>();
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if (map[i] == 0) {
|
||||
queue.add(i);
|
||||
}
|
||||
}
|
||||
// 出对,并去边
|
||||
int exeCount = 0;
|
||||
while (!queue.isEmpty()) {
|
||||
Integer pop = queue.pop();
|
||||
exeCount++;
|
||||
// 遍历所有的边
|
||||
for (int i = 0; i < prerequisites.length; i++) {
|
||||
if (prerequisites[i][1] == pop) {
|
||||
int deleteSideNode = prerequisites[i][0];
|
||||
map[deleteSideNode]--;
|
||||
if (map[deleteSideNode] == 0) {
|
||||
queue.add(deleteSideNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 如果队列中没有元素了,但还有边,返回false
|
||||
return exeCount == numCourses;
|
||||
}
|
||||
}
|
||||
}
|
98
src/main/java/cn/whaifree/leetCode/Graph/LeetCode210.java
Normal file
98
src/main/java/cn/whaifree/leetCode/Graph/LeetCode210.java
Normal file
@ -0,0 +1,98 @@
|
||||
package cn.whaifree.leetCode.Graph;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/16 0:44
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode210 {
|
||||
@Test
|
||||
public void test() {
|
||||
int[][] prerequisites = {{1,0},{2,0},{3,1},{3,2}};
|
||||
int[] res = new Solution().findOrder(4, prerequisites);
|
||||
System.out.println(Arrays.toString(res));
|
||||
}
|
||||
|
||||
public int[] findOrder(int numCourses, int[][] prerequisites) {
|
||||
if (numCourses == 0) return new int[0];
|
||||
int[] inDegrees = new int[numCourses];
|
||||
|
||||
|
||||
// 建立入度表
|
||||
for (int[] p : prerequisites) {
|
||||
inDegrees[p[0]]++; // 记录每个节点的入度
|
||||
}
|
||||
// 入度为0的节点队列
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
for (int i = 0; i < inDegrees.length; i++) {
|
||||
if (inDegrees[i] == 0) queue.offer(i); // 入度为 0 的节点可以进行执行
|
||||
}
|
||||
int count = 0; // 记录可以执行的任务数
|
||||
int[] res = new int[numCourses]; // 完整拓扑排序的执行过程
|
||||
|
||||
// 根据提供的可以执行的任务(入度为 0),删除入度为 0 的节点
|
||||
while (!queue.isEmpty()){
|
||||
int curr = queue.poll(); // 拿到一个可以执行的任务
|
||||
res[count++] = curr; // 这个任务可以执行,作为下一次执行的节点
|
||||
for (int[] p : prerequisites) {
|
||||
if (p[1] == curr){ // {a,b} 表示 a 依赖 b b-->a
|
||||
inDegrees[p[0]]--;
|
||||
if (inDegrees[p[0]] == 0) queue.offer(p[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count == numCourses) return res;
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
|
||||
class Solution {
|
||||
public int[] findOrder(int numCourses, int[][] prerequisites) {
|
||||
List<List<Integer>> graph = new ArrayList<>();
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
graph.add(new ArrayList<>());
|
||||
}
|
||||
int[] inGre = new int[numCourses];
|
||||
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
for (int[] prerequisite : prerequisites) {
|
||||
int course = prerequisite[0];
|
||||
int pre = prerequisite[1];
|
||||
inGre[course]++;
|
||||
graph.get(pre).add(course);
|
||||
}
|
||||
|
||||
for (int i = 0; i < inGre.length; i++) {
|
||||
if (inGre[i] == 0) {
|
||||
deque.add(i);
|
||||
}
|
||||
}
|
||||
|
||||
int exec = 0;
|
||||
int[] res = new int[numCourses];
|
||||
while (!deque.isEmpty()) {
|
||||
Integer exe = deque.pop();
|
||||
res[exec] = exe;
|
||||
exec++;
|
||||
List<Integer> in = graph.get(exe);
|
||||
for (int into = 0; into < in.size(); into++) {
|
||||
Integer intoNode = in.get(into);
|
||||
inGre[intoNode]--;
|
||||
if (inGre[intoNode] == 0) {
|
||||
deque.add(intoNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numCourses == exec) {
|
||||
return res;
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package cn.whaifree.redo.redo_all_240721;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/9/16 1:01
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode583 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String word1 = "leetcode";
|
||||
String word2 = "etco";
|
||||
System.out.println(new Solution().minDistance(word1, word2));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
public int minDistance(String word1, String word2) {
|
||||
char[] w1 = word1.toCharArray();
|
||||
char[] w2 = word2.toCharArray();
|
||||
int[][] dp = new int[w1.length + 1][w2.length + 1];
|
||||
for (int i = 0; i <= w2.length; i++) {
|
||||
dp[0][i] = i;
|
||||
}
|
||||
for (int j = 0; j <= w1.length; j++) {
|
||||
dp[j][0] = j;
|
||||
}
|
||||
/**
|
||||
* '' s e a
|
||||
* '' 0 1 2 3
|
||||
* e 1 2 1 2 不配 左边|上 +1 配 对i-1 j-1
|
||||
* a 2 3 2
|
||||
* t 3
|
||||
*
|
||||
* @param word1
|
||||
* @param word2
|
||||
* @return
|
||||
*/
|
||||
|
||||
for (int i = 0; i < w1.length; i++) {
|
||||
for (int j = 0; j < w2.length; j++) {
|
||||
if (w1[i] == w2[j]) {
|
||||
dp[i + 1][j + 1] = dp[i][j];
|
||||
}else {
|
||||
dp[i + 1][j + 1] = Math.min(dp[i][j + 1], dp[i + 1][j]) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[w1.length][w2.length];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user