This commit is contained in:
whai 2024-02-24 12:56:53 +08:00
parent a9c9c4cf30
commit b8be07776c
6 changed files with 342 additions and 28 deletions

View File

@ -1,50 +1,64 @@
package cn.whaifree.redo.redo_24_2_22;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import org.w3c.dom.ls.LSException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/22 11:25
* @Date 2024/2/23 11:56
* @注释
*/
public class LeetCode131 {
@Test
public void test(
) {
TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
public void test() {
new Solution().partition("aab").forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return root;
List<List<String>> res = new ArrayList<>();
LinkedList<String> path = new LinkedList<>();
public List<List<String>> partition(String s) {
backTracking(s, 0, s.length() - 1);
return res;
}
public void backTracking(String s, int start, int end) {
if (start >= s.length()) {
res.add(new ArrayList<>(path));
return;
}
if (root.val == p.val) {
return root;
}
if (root.val == q.val) {
return root;
}
TreeNode right = lowestCommonAncestor(root.right, p, q);
TreeNode left = lowestCommonAncestor(root.left, p, q);
// 这里用于输出公共父节点
// 某个节点第一个出现的左边有p或者q右边也有p或者q则就是公共父节点
if (right != null && left != null) {
return root;
}
if (right != null) {
return right;
} else {
return left;
for (int i = start; i < s.length(); i++) {
if (isHuiWen(s, start, i)) {
path.add(s.substring(start, i + 1));
backTracking(s, i + 1, end);
path.removeLast();
}
}
}
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;
}
}
}

View File

@ -0,0 +1,50 @@
package cn.whaifree.redo.redo_24_2_22;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/22 11:25
* @注释
*/
public class LeetCode236 {
@Test
public void test(
) {
TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4);
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return root;
}
if (root.val == p.val) {
return root;
}
if (root.val == q.val) {
return root;
}
TreeNode right = lowestCommonAncestor(root.right, p, q);
TreeNode left = lowestCommonAncestor(root.left, p, q);
// 这里用于输出公共父节点
// 某个节点第一个出现的左边有p或者q右边也有p或者q则就是公共父节点
if (right != null && left != null) {
return root;
}
if (right != null) {
return right;
} else {
return left;
}
}
}
}

View File

@ -0,0 +1,75 @@
package cn.whaifree.redo.redo_24_2_22;
import org.junit.Test;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/23 13:19
* @注释
*/
public class LeetCode46 {
@Test
public void test() {
new Solution().permute(new int[]{1, 2, 3}).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution
{
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used = null;
public List<List<Integer>> permute(int[] nums) {
used = new boolean[nums.length];
backTracking(nums);
return res;
}
void backTracking(int[] nums) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
// 跳过子树路径上同一个
if (used[i] == true) {
continue;
}
path.add(nums[i]);
used[i] = true;
backTracking(nums);
used[i] = false;
path.removeLast();
}
}
}
public static void main(String[] args) {
// 获取 Java 线程管理 MXBean
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
// 不需要获取同步的 monitor synchronizer 信息仅获取线程和线程堆栈信息
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
// 遍历线程信息仅打印线程 ID 和线程名称信息
for (ThreadInfo threadInfo : threadInfos) {
System.out.println("[" + threadInfo.getThreadId() + "] " + threadInfo.getThreadName());
}
}
}

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redo_24_2_22;
import org.junit.Test;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/23 13:43
* @注释
*/
public class LeetCode47 {
@Test
public void test() {
new Solution().permuteUnique(new int[]{1,1,2}).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used = null;
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
used = new boolean[nums.length];
backTracking(nums);
return res;
}
public void backTracking(int[] nums) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if ((i > 0 && nums[i] == nums[i - 1] && used[i-1] == false)) {
continue;
}
if (used[i]) {
continue;
}
used[i] = true;
path.add(nums[i]);
backTracking(nums);
used[i] = false;
path.removeLast();
}
}
}
}

View File

@ -0,0 +1,59 @@
package cn.whaifree.redo.redo_24_2_22;
import org.junit.Test;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/23 12:10
* @注释
*/
public class LeetCode491 {
@Test
public void test() {
new Solution().findSubsequences(new int[]{1, 5, 3, 3}).forEach(
list -> {
System.out.println(list);
}
);
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> path = new LinkedList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums, 0);
return res;
}
public void backTracking(int[] nums, int start) {
if (path.size() > 1) {
// 插入res
res.add(new ArrayList<>(path));
}
// 每层跳过不能排序所以不能使用used
Set<Integer> set = new HashSet<>();
for (int i = start; i < nums.length; i++) {
// 如果 递减跳过
if ((!path.isEmpty() && path.getLast() > nums[i]) || set.contains(nums[i])) {
continue;
}
set.add(nums[i]);
path.add(nums[i]);
backTracking(nums, i + 1);
path.removeLast();
}
}
}
}

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redo_24_2_22;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/23 10:02
* @注释
*/
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<>();
LinkedList<Integer> path = new LinkedList<>();
boolean[] used = null;
public List<List<Integer>> subsetsWithDup(int[] nums) {
used = new boolean[nums.length];
Arrays.sort(nums);
backTracking(nums, 0);
return res;
}
public void backTracking(int[] nums, int start) {
res.add(new ArrayList<>(path));
if (start >= nums.length) {
return;
}
for (int i = start; i < nums.length; i++) {
// 兄弟节点被使用完了就是falsecontinue
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
used[i] = true;
path.add(nums[i]);
backTracking(nums, i + 1);
used[i] = false;
path.removeLast();
}
}
}
}