7.5 图论+回溯
This commit is contained in:
parent
a5d2dc48af
commit
b8f430ad10
@ -9,19 +9,44 @@ import java.util.List;
|
||||
*示例 1:
|
||||
* 输入:nums = [1,2,3]
|
||||
* 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||
*
|
||||
|
||||
* 示例 2:
|
||||
* 输入:nums = [0,1]
|
||||
* 输出:[[0,1],[1,0]]
|
||||
*
|
||||
|
||||
* 示例 3:
|
||||
* 输入:nums = [1]
|
||||
* 输出:[[1]]
|
||||
*
|
||||
|
||||
* 链接:https://leetcode.cn/problems/permutations/
|
||||
|
||||
*/
|
||||
//二刷会做
|
||||
public class Permute {
|
||||
void dfs(List<List<Integer>>res,List<Integer>path,int[] nums,boolean[] used){
|
||||
if(path.size()==nums.length) {
|
||||
res.add(new ArrayList<>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if(used[i])continue;
|
||||
used[i]=true;
|
||||
path.add(nums[i]);
|
||||
dfs(res,path,nums,used);
|
||||
path.remove(path.size()-1);
|
||||
used[i]=false;
|
||||
}
|
||||
}
|
||||
public List<List<Integer>> permute2(int[] nums) {
|
||||
List<List<Integer>>res=new ArrayList<>();
|
||||
List<Integer>path=new ArrayList<>();
|
||||
boolean[] used=new boolean[nums.length];
|
||||
dfs(res,path,nums,used);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<List<Integer>> permute(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
// 用来标记数组中数字是否被使用
|
||||
|
@ -18,7 +18,9 @@ import java.util.List;
|
||||
|
||||
* 链接:https://leetcode.cn/problems/subsets/
|
||||
*/
|
||||
//二刷会做
|
||||
public class Subsets {
|
||||
|
||||
public List<List<Integer>> subsets(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
List<Integer> path = new ArrayList<>();
|
||||
|
@ -17,6 +17,7 @@ import java.util.List;
|
||||
*/
|
||||
//不会,有点难
|
||||
public class SubsetsWithDup {
|
||||
|
||||
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
List<Integer> path = new ArrayList<>();
|
||||
@ -43,7 +44,7 @@ public class SubsetsWithDup {
|
||||
// 测试方法
|
||||
public static void main(String[] args) {
|
||||
SubsetsWithDup solution = new SubsetsWithDup();
|
||||
int[] nums = {1, 2, 2};
|
||||
int[] nums = {1, 2, 3};
|
||||
List<List<Integer>> subsets = solution.subsetsWithDup(nums);
|
||||
System.out.println(subsets);
|
||||
// 输出:[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
|
||||
|
@ -15,6 +15,7 @@ import java.util.Queue;
|
||||
* 链接:https://leetcode.cn/problems/course-schedule/
|
||||
|
||||
*/
|
||||
|
||||
//思路知道,代码不好写
|
||||
/*
|
||||
思路解析
|
||||
@ -32,6 +33,7 @@ import java.util.Queue;
|
||||
判断是否存在环
|
||||
如果最终能处理的课程数量等于总课程数,则说明不存在环路,所有课程都可以按要求完成;否则说明存在循环依赖,无法完成所有课程。
|
||||
*/
|
||||
//二刷不会
|
||||
public class CanFinish {
|
||||
public boolean canFinish(int numCourses, int[][] prerequisites) {
|
||||
// 构建图:graph.get(i) 存储课程 i 的后续课程
|
||||
|
@ -1,9 +1,6 @@
|
||||
package graph;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 题目: 210. 课程表 II (findOrder)
|
||||
@ -19,7 +16,41 @@ import java.util.Queue;
|
||||
* 链接:https://leetcode.cn/problems/course-schedule-ii/
|
||||
|
||||
*/
|
||||
//二刷会做(先把207做了就会)
|
||||
public class FindOrder {
|
||||
public int[] findOrder2(int numCourses, int[][] prerequisites) {
|
||||
int[] indgree=new int[numCourses];
|
||||
List<List<Integer>>graph=new ArrayList<>();
|
||||
int[] res=new int[numCourses];
|
||||
Queue<Integer>queue=new LinkedList<>();
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
graph.add(new ArrayList<>());
|
||||
}
|
||||
for (int i = 0; i < prerequisites.length; i++) {
|
||||
int course=prerequisites[i][0];
|
||||
int precourse=prerequisites[i][1];
|
||||
indgree[course]++;
|
||||
graph.get(precourse).add(course);
|
||||
}
|
||||
for (int i = 0; i < numCourses; i++) {
|
||||
if(indgree[i]==0)
|
||||
queue.add(i);
|
||||
}
|
||||
int count=0;
|
||||
while (!queue.isEmpty()){
|
||||
int precourse=queue.poll();
|
||||
res[count]=precourse;
|
||||
count++;
|
||||
for (Integer course : graph.get(precourse)) {
|
||||
indgree[course]--;
|
||||
if(indgree[course]==0)
|
||||
queue.add(course);
|
||||
}
|
||||
}
|
||||
if(count==numCourses) return res;
|
||||
else return new int[0];
|
||||
}
|
||||
|
||||
public int[] findOrder(int numCourses, int[][] prerequisites) {
|
||||
int[] indegree=new int[numCourses];
|
||||
int[] res=new int[numCourses];
|
||||
|
@ -1,4 +1,4 @@
|
||||
package graph;
|
||||
package 实现类功能;
|
||||
/**
|
||||
* 题目: 208. 实现 Trie (前缀树)
|
||||
* 描述:Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补全和拼写检查。
|
||||
@ -33,6 +33,7 @@ package graph;
|
||||
* 共用方法 (searchPrefix)
|
||||
* 将插入和查找里的公共逻辑抽取出来:遍历路径、空指针检查、节点推进。
|
||||
*/
|
||||
//二刷不会 学习
|
||||
//https://leetcode.cn/problems/implement-trie-prefix-tree/solutions/98390/trie-tree-de-shi-xian-gua-he-chu-xue-zhe-by-huwt/?envType=study-plan-v2&envId=top-interview-150
|
||||
class Trie {
|
||||
// children 数组保存当前节点的所有子节点,索引 0-25 分别对应 'a'-'z'
|
14
src/test/java/backtrack/SubsetsTest.java
Normal file
14
src/test/java/backtrack/SubsetsTest.java
Normal file
@ -0,0 +1,14 @@
|
||||
package backtrack;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SubsetsTest extends TestCase {
|
||||
|
||||
public void testSubsets() {
|
||||
int[]nums = {1,2,3};
|
||||
Subsets solution = new Subsets();
|
||||
List<List<Integer>> subsets = solution.subsets(nums);
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ public class SubsetsWithDupTest {
|
||||
|
||||
@Test
|
||||
public void subsetsWithDup() {
|
||||
int[]nums = {1,2,2};
|
||||
int[]nums = {1,2,3};
|
||||
SubsetsWithDup solution = new SubsetsWithDup();
|
||||
List<List<Integer>>res=solution.subsetsWithDup(nums);
|
||||
System.out.println(res);
|
||||
|
Loading…
x
Reference in New Issue
Block a user