diff --git a/src/main/java/cn/whaifree/interview/qz/suiyou/p1.java b/src/main/java/cn/whaifree/interview/qz/suiyou/p1.java new file mode 100644 index 0000000..2a2d3f2 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/qz/suiyou/p1.java @@ -0,0 +1,10 @@ +package cn.whaifree.interview.qz.suiyou; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 19:18 + * @注释 + */ +public class p1 { +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java index f2493ed..5dc3f51 100644 --- a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java @@ -1,11 +1,7 @@ package cn.whaifree.leetCode.Greedy; -import cn.whaifree.leetCode.Tree.LeetCode94; import org.junit.Test; - -import java.net.URL; - /** * @version 1.0 * @Author whai文海 @@ -18,8 +14,27 @@ public class LeetCode55 { public void test() { System.out.println(new Solution().canJump(new int[]{2,0,0})); + char c = 10 + 90; + System.out.println(c); + long g = 9999999999L; + System.out.println(g); + } + public static void main(String[] args) { + System.out.println(i()); + + } + + public static int i() { + try { + + return 1; + } catch (Exception e) { + + } finally { + return 0; + } } diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LC15.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LC15.java new file mode 100644 index 0000000..e80ed01 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LC15.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 10:25 + * @注释 + */ +public class LC15{ + + @Test + public void test() { + Solution solution = new Solution(); + int[] nums = {-1, 0, 1, 2, -1, -4}; + List> lists = solution.threeSum(nums); + System.out.println(lists); + } + + class Solution { + + List> res = new ArrayList<>(); + public List> threeSum(int[] nums) { + return threeSum(nums, 0); + } + + + public List> threeSum(int[] nums, int target) { + Arrays.sort(nums); + for (int left = 0; left < nums.length - 2; left++) { + + if (left > 0 && nums[left] == nums[left - 1]) { + continue; + } + + int right = nums.length - 1; + int mid = left + 1; + + while (mid < right) { + int tmpSum = nums[left] + nums[mid] + nums[right]; + if (tmpSum == target) { + res.add(Arrays.asList(nums[left], nums[mid], nums[right])); + while (mid < right && nums[mid] == nums[mid + 1]) { + mid++; + } + while (mid < right && nums[right] == nums[right - 1]) { + right--; + } + mid++; + right--; + }else if (tmpSum > target) { + right--; + } else { + mid++; + } + + + } + } + return res; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode131.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode131.java new file mode 100644 index 0000000..2708a76 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode131.java @@ -0,0 +1,61 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 12:46 + * @注释 + */ +public class LeetCode131 { + @Test + public void test() { + Solution solution = new Solution(); + List> lists = solution.partition("aab"); + System.out.println(lists); + } + + + class Solution { + + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + public List> partition(String s) { + backTracking(s, 0); + return res; + } + + public void backTracking(String s, int start) { + if (start >= s.length()) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = start; i < s.length(); i++) { + if (isHuiWen(s, start, i)) { + path.add(s.substring(start, i + 1)); + backTracking(s, i + 1); + path.remove(path.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; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode209.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode209.java new file mode 100644 index 0000000..ee4e605 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode209.java @@ -0,0 +1,95 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 10:41 + * @注释 + */ +public class LeetCode209 { + + @Test + public void test() { + int[] nums = {1,4,4}; + int target = 4; + int i = new Solution().minSubArrayLen(target, nums); + System.out.println(i); + } + + + class Solution { + public int minSubArrayLen(int target, int[] nums) { + int right = 0; + int left = 0; + int nowSum = 0; + int res = Integer.MAX_VALUE; + while (right < nums.length) { + nowSum += nums[right]; + while (left <= right && nowSum >= target) { + res = Math.min(res, right - left + 1); + nowSum -= nums[left]; + left++; + } + right++; + } + return res == Integer.MAX_VALUE ? 0 : res; + } + } + + public static void main(String[] args) { + int[] ints = {1, 2, 3, 4, 9, 10}; + int i = Arrays.binarySearch(ints, 6); + System.out.println(i); + } + + @Test + public void test1() { + int[] nums = {2, 3, 1, 2, 4, 3}; + int target = 7; + + int i = new Solution1().minSubArrayLen(target, nums); + System.out.println(i); + } + + class Solution1 { + + + /** + * | x | target | + * 0 n i + * @param target + * @param nums + * @return + */ + + public int minSubArrayLen(int target, int[] nums) { + + int[] preSum = new int[nums.length]; + preSum[0] = nums[0]; + for (int i = 1; i < nums.length; i++) { + preSum[i] = preSum[i - 1] + nums[i]; + } + + int res = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + int A = preSum[i]; + if (A < target) { + continue; + } + int tar = A - target; + int index = Arrays.binarySearch(preSum, tar); + if (index < 0) { + index = -index - 1; + } else { + index = index + 1; + } + res = Math.min(res, i - index + 1); + } + return res == Integer.MAX_VALUE ? 0 : res; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode222.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode222.java new file mode 100644 index 0000000..515ad3c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode222.java @@ -0,0 +1,46 @@ +package cn.whaifree.redo.redo_all_240924; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 12:05 + * @注释 + */ +public class LeetCode222 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(1, 2, 3, 4); + System.out.println(new Solution().countNodes(treeNode)); + } + + class Solution { + public int countNodes(TreeNode root) { + + if (root == null) { + return 0; + } + + TreeNode left = root; + int leftCount = 0; + while (left != null) { + leftCount++; + left = left.left; + } + TreeNode right = root; + int rightCount = 0; + while (right != null) { + rightCount++; + right = right.right; + } + + if (leftCount == rightCount) { + return (2 << (leftCount - 1)) - 1; + } + return countNodes(root.left) + countNodes(root.right) + 1; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode276.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode276.java new file mode 100644 index 0000000..d1068a5 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode276.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 13:51 + * @注释 + */ +public class LeetCode276 { + + @Test + public void test() { + int[] nums = {0,0,0}; + int res = new Solution().wiggleMaxLength(nums); + System.out.println(res); + + } + class Solution { + public int wiggleMaxLength(int[] nums) { + if (nums.length < 2) { + return nums.length; + } + + int preSum = 0; + int nowSum = 0; + + int count = 1; + for (int i = 1; i < nums.length; i++) { + nowSum = nums[i] - nums[i - 1]; + if ((nowSum > 0 && preSum <= 0) || (nowSum < 0 && preSum >= 0)) { + // 方向相反 + count++; + preSum = nowSum; + } else { + // 方向相同 + + } + } + return count; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode40.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode40.java new file mode 100644 index 0000000..9cdd03e --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode40.java @@ -0,0 +1,67 @@ +package cn.whaifree.redo.redo_all_240924; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 12:14 + * @注释 + */ +public class LeetCode40 { + + @Test + public void test() { + int[] candidates = {10,1,2,7,6,1,5}; + int target = 8; + Solution solution = new Solution(); + List> lists = solution.combinationSum2(candidates, target); + System.out.println(lists); + } + + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + int nowSum = 0; + boolean[] used; + public List> combinationSum2(int[] candidates, int target) { + used = new boolean[candidates.length]; + Arrays.sort(candidates); + backTracking(candidates, 0, target); + return res; + } + + public void backTracking(int[] candidates, int start, int target) { + if (target == nowSum) { + res.add(new ArrayList<>(path)); + return; + } + if (start > candidates.length) { + return; + } + + + for (int i = start; i < candidates.length; i++) { + if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) { + continue; + } + if (nowSum > target) { + return; + } + used[i] = true; + path.add(candidates[i]); + nowSum += candidates[i]; + backTracking(candidates, i + 1, target); + nowSum -= candidates[i]; + path.remove(path.size() - 1); + used[i] = false; + } + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode94.java b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode94.java new file mode 100644 index 0000000..1d41549 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240924/LeetCode94.java @@ -0,0 +1,85 @@ +package cn.whaifree.redo.redo_all_240924; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/24 11:40 + * @注释 + */ +public class LeetCode94 { + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(new Integer[]{2, 3, 6, 8, 9}); + System.out.println(new Solution().inorderTraversal(treeNode)); + } + + class Solution { + public List inorderTraversal(TreeNode root) { + if (root == null) { + return new ArrayList<>(); + } + + List list = new ArrayList<>(); + Deque deque = new LinkedList<>(); + deque.push(root); + while (!deque.isEmpty()) { + TreeNode pop = deque.pop(); + if (pop != null) { + if (pop.left != null) { + deque.push(pop.left); + } + if (pop.right != null) { + deque.push(pop.right); + } + deque.push(pop); + deque.push(null); + + }else { + list.add(deque.pop().val); + } + } + Collections.reverse(list); + return list; + } + } + + @Test + public void test2() { + TreeNode treeNode = TreeNode.constructTreeByArray(new Integer[]{2, 3, 6, 8, 9}); + System.out.println(Level.levelOrder(treeNode)); + } + + + class Level{ + public static List> levelOrder(TreeNode root) { + List> res = new ArrayList<>(); + Deque deque = new LinkedList<>(); + deque.add(root); + while (!deque.isEmpty()) { + + int size = deque.size(); + ArrayList e = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TreeNode pop = deque.pop(); + if (pop.left != null) { + deque.add(pop.left); + } + if (pop.right != null) { + deque.add(pop.right); + } + e.add(pop.val); + } + res.add(e); + + } + + return res; + } + + } +}