This commit is contained in:
whai 2024-02-19 10:48:07 +08:00
parent 508bf989fc
commit 17bbb0f3ed
5 changed files with 412 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/18 16:41
* @注释
*/
public class LeetCode491 {
@Test
public void test() {
new Solution().findSubsequences(new int[]{4,6,7,5,7}).forEach(list -> {
System.out.println(list);
});
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums, 0);
return res;
}
public void backTracking(int[] nums, int index) {
if (path.size() > 1) {
// 插入res
res.add(new ArrayList<>(path));
}
Set<Integer> set = new HashSet<>();
for (int i = index; i < nums.length; i++) {
// 如果不满足非递减即递增这个子树不保留
if ((!path.isEmpty() && path.get(path.size() - 1) > nums[i]) // 递增
|| set.contains(nums[i]) // [4,7,xx,7],[4,7]已经出现在path中7加入了set那么在本层后面遇到7的时候就直接continues
) {
continue;
}
set.add(nums[i]);
path.add(nums[i]);
backTracking(nums, i + 1);
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,44 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/18 10:29
* @注释
*/
public class LeetCode78 {
@Test
public void test() {
System.out.println(new Solution().subsets(new int[]{1, 2, 3}));
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
backTracking(nums, 0);
return res;
}
void backTracking(int[] nums, int start) {
res.add(new ArrayList<>(temp));
if (start >= nums.length) {
return;
}
for (int i = start; i < nums.length ; i++) {
temp.add(nums[i]);
backTracking(nums,i+1);
temp.remove(temp.size() - 1);
}
}
}
}

View File

@ -0,0 +1,66 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/18 13:26
* @注释
*/
public class LeetCode90 {
@Test
public void test() {
new Solution().subsetsWithDup(new int[]{1, 2, 2}).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
boolean[] used = null;
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
used = new boolean[nums.length];
backTracking(nums, 0);
return res;
}
void backTracking(int[] nums, int start) {
res.add(new ArrayList<>(temp));
if (start >= nums.length) {
return;
}
for (int i = start; i < nums.length ; i++) {
// 1. u1 u2 相同并且u1曾经被调用过则不继续进行本次循环
// if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
// continue;
// }
// 2. 不使用used数组跳过当前树层使用过的相同的元素
if ( i > start && nums[i - 1] == nums[i] ) {
continue;
}
temp.add(nums[i]);
used[i] = true;
backTracking(nums,i+1);
temp.remove(temp.size() - 1);
used[i] = false;
}
}
}
}

View File

@ -0,0 +1,176 @@
package cn.whaifree.leetCode.BackTracking;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/18 9:27
* @注释
*/
public class LeetCode93 {
@Test
public void test() {
for (String restoreIpAddress : new Solution1().restoreIpAddresses("101023")) {
System.out.println(restoreIpAddress);
}
}
class Solution {
List<String> res = new ArrayList<>();
List<String> path = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
backTracking(s, 0, 0);
return res;
}
/**
*
* @param s
* @param start 递归开始标记
* @param number 这是第几个切片4个切片为一组有效返回
*/
public void backTracking(String s, int start,int number) {
// 如果 第5个切片了则存在多余的字符直接return
if (number > 4) {
return;
}
if (start >= s.length() && path.size() == 4) {
res.add(String.join(".", path));
// StringBuilder stringBuilder = new StringBuilder();
// for (int i = 0; i < path.size() - 1; i++) {
// stringBuilder.append(path.get(i)).append(".");
// }
// stringBuilder.append(path.get(path.size() - 1));
// res.add(stringBuilder.toString());
return;
}
for (int i = start; i < s.length() ; i++) {
if (isValid(s, start, i + 1)) {
path.add(s.substring(start, i + 1));
backTracking(s, i + 1, number + 1);
path.remove(path.size() - 1);
}else {
// 2565 256无效那么2565直接就无效了
break;
}
}
}
public boolean isValid(String s, int start, int end) {
String substring = s.substring(start, end);
// 子串最多不超过3个字符
if (substring.length() > 3 ) {
return false;
}
// 01为非法"0"为合法
if (substring.length() != 1 && substring.startsWith("0")) {
return false;
}
int integer = Integer.parseInt(substring);
if (integer >= 0 && integer <= 255) {
return true;
}
return false;
}
/**
* 通过char进行字符串转为int
* @param s
* @param start
* @param end
* @return
*/
private boolean isValid(StringBuilder s, int start, int end){
if(start > end)
return false;
if(s.charAt(start) == '0' && start != end)
return false;
int num = 0;
for(int i = start; i <= end; i++){
int digit = s.charAt(i) - '0';
num = num * 10 + digit;
if(num > 255)
return false;
}
return true;
}
}
class Solution1 {
List<String> res = new ArrayList<>();
List<String> path = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
backTracking(s, 0);
return res;
}
/**
*
* @param s
* @param start 递归开始标记
*/
public void backTracking(String s, int start) {
if (start >= s.length() && path.size() == 4) {
res.add(String.join(".", path));
// StringBuilder stringBuilder = new StringBuilder();
// for (int i = 0; i < path.size() - 1; i++) {
// stringBuilder.append(path.get(i)).append(".");
// }
// stringBuilder.append(path.get(path.size() - 1));
// res.add(stringBuilder.toString());
return;
}
for (int i = start; i < s.length() ; i++) {
if (isValid(s, start, i + 1)) {
path.add(s.substring(start, i + 1));
backTracking(s, i + 1);
path.remove(path.size() - 1);
}else {
// 2565 256无效那么2565直接就无效了
break;
}
}
}
public boolean isValid(String s, int start, int end) {
String substring = s.substring(start, end);
// 子串最多不超过3个字符
if (substring.length() > 3 ) {
return false;
}
// 01为非法"0"为合法
if (substring.length() != 1 && substring.startsWith("0")) {
return false;
}
int integer = Integer.parseInt(substring);
if (integer >= 0 && integer <= 255) {
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,67 @@
package cn.whaifree.redo.redo_24_2_18;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/18 9:04
* @注释
*/
public class LeetCode131 {
@Test
public void test() {
String abc = "aab";
new Solution().partition(abc).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
List<List<String>> res = new ArrayList<>();
List<String> list = new ArrayList<>();
public List<List<String>> partition(String s) {
backTracing(s,0);
return res;
}
public void backTracing(String s,int start) {
// 遍历完了树到根部了就记为一次结果
if (start >= s.length()) {
res.add(new ArrayList<>(list));
}
for (int i = start; i < s.length(); i++) {
// 只有当是回文串时才进行下一次切割
if (isHuiWen(s, start, i)) {
String substring = s.substring(start, i + 1);
list.add(substring);
backTracing(s, i + 1);
list.remove(list.size() - 1);
}
}
}
public boolean isHuiWen(String s, int start, int end) {
while (start < end) {
if (s.charAt(start) != s.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
}
}