From a10c05498ca8ba10e4201c53f680db26401d8b40 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Wed, 4 Sep 2024 00:46:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=86LeetCode?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E6=B1=82=E8=A7=A3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/whaifree/redo/redoAll/LeetCode300.java | 79 ++++++++++++++ .../whaifree/redo/redoAll/LeetCode337_1.java | 45 ++++++++ .../cn/whaifree/redo/redoAll/LeetCode392.java | 75 +++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode674.java | 61 +++++++++++ .../cn/whaifree/redo/redoAll/LeetCode82.java | 39 +++++++ .../whaifree/redo/redoAll/LeetCode84_1.java | 100 ++++++++++++++++++ 6 files changed, 399 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode300.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode337_1.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode392.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode674.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode82.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode84_1.java diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode300.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode300.java new file mode 100644 index 0000000..c37d58d --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode300.java @@ -0,0 +1,79 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/3 22:41 + * @注释 + */ +public class LeetCode300 { + + @Test + public void test() { + int[] nums = {10,9,2,5,3,7,101,18}; + Solution1 solution = new Solution1(); + int i = solution.lengthOfLIS(nums); + System.out.println(i); + } + + class Solution { + + + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + + /** + * **dp[i]表示i之前包括i的以nums[i]结尾(每次都需要使用nums[j]来同nums[i]比较)的最长递增子序列的长度** + * 子序列 确定某个数,让前面不断与他比较 + */ + for (int i = 1; i < nums.length; i++) { + for (int j = i; j >= 0; j--) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + return Arrays.stream(dp).max().getAsInt(); + } + + + } + + class Solution1 { + + /** + * dp[i] 表示0-i已经出现的最长 + * + * [10,9,2,5,3,7,101,18] + * 1 1 1 2 2 3 4 4 + * + * @param nums + * @return + */ + public int lengthOfLIS(int[] nums) { + + + int[] dp = new int[nums.length]; + + Arrays.fill(dp, 1); + for (int i = 1; i < nums.length; i++) { + + // 不断往后搜索,如果比当前值小,就有可能是更长的值 + int index = i - 1; + while (index >= 0) { + if (nums[i] > nums[index]) { + dp[i] = Math.max(dp[index] + 1, dp[i]); + } + index--; + } + + } + return Arrays.stream(dp).max().getAsInt(); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode337_1.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode337_1.java new file mode 100644 index 0000000..03df00f --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode337_1.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/2 21:00 + * @注释 + */ +public class LeetCode337_1 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTreeByArray(3, 4, 5, 1, 3, null, 1); + System.out.println(new Solution().rob(root)); + } + + class Solution { + public int rob(TreeNode root) { + int[] circle = circle(root); + return Math.max(circle[0], circle[1]); + } + + /** + * + * @param root + * @return int[0] 为不抢该节点获得最大利润,int[1]为抢获得最大利润 + */ + public int[] circle(TreeNode root) { + + if (root == null) { + return new int[2]; + } + + int[] left = circle(root.left); + int[] right = circle(root.right); + + int a = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); // 不抢这个节点 + int b = left[0] + right[0] + root.val; // 抢这个节点 + return new int[]{a, b}; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode392.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode392.java new file mode 100644 index 0000000..41a8a7a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode392.java @@ -0,0 +1,75 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/3 23:49 + * @注释 + */ +public class LeetCode392 { + @Test + public void test() { + String s = "aaaaaa"; + String t = "bbaaaa"; + Solution1 solution = new Solution1(); + boolean subsequence = solution.isSubsequence(s, t); + System.out.println(subsequence); + } + + class Solution { + public boolean isSubsequence(String s, String t) { + char[] sCharArray = s.toCharArray(); + char[] tCharArray = t.toCharArray(); + int index = 0; + for (int i = 0; i < sCharArray.length; i++) { + boolean find = false; + while (index < tCharArray.length) { + if (sCharArray[i] == tCharArray[index]) { + find = true; + index++; + break; + }else { + index++; + } + } + if (!find) { + return false; + } + } + return true; + } + } + + class Solution1 { + /** + * dp[i][j] 表示s的i在t的j中出现的长度 + * + * "abc" "ahbgdc" + * a h b q d c + * 0 + * a 0 1 1 1 1 1 1 相等,取上+1 + * b 0 0 0 2 + * c 0 + * @param s + * @param t + * @return + */ + public boolean isSubsequence(String s, String t) { + int[][] dp = new int[s.length() + 1][t.length() + 1]; + + for (int i = 1; i <= s.length(); i++) { + for (int j = 1; j <= t.length(); j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = dp[i][j - 1]; + } + } + } + + return dp[s.length()][t.length()] == s.length(); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode674.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode674.java new file mode 100644 index 0000000..8249dc8 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode674.java @@ -0,0 +1,61 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/4 0:09 + * @注释 + */ +public class LeetCode674 { + + @Test + public void test() { + int[] nums = {1,3,5,4,7}; + System.out.println(new Solution1().findLengthOfLCIS(nums)); + } + + class Solution { + public int findLengthOfLCIS(int[] nums) { + int max = 0; + int left = 0; + int right = 0; + while (right < nums.length - 1) { + if (nums[right] < nums[right + 1]) { + right++; + }else { + max = Math.max(max, right - left + 1); + right++; + left = right; + } + } + + return Math.max(max, right - left + 1); + } + } + + class Solution1 { + /** + * dp[i] 表示i目前的最长连续最长递增长度 + * 1,3,5,4,7 + * 1 2 3 1 2 + * @param nums + * @return + */ + public int findLengthOfLCIS(int[] nums) { + int[] dp = new int[nums.length]; + dp[0] = 1; + for (int i = 1; i < nums.length; i++) { + if (nums[i] > nums[i - 1]) { + dp[i] = dp[i - 1] + 1; + }else { + dp[i] = 1; + } + } + return Arrays.stream(dp).max().getAsInt(); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode82.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode82.java new file mode 100644 index 0000000..3fc1473 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode82.java @@ -0,0 +1,39 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/4 0:24 + * @注释 + */ +public class LeetCode82 { + + @Test + public void test() { + new Solution().deleteDuplicates(ListNode.listNodeFromArray(new int[]{1,2,3,3,4,4,5,5})).printList(); + } + + class Solution { + public ListNode deleteDuplicates(ListNode head) { + ListNode pre = new ListNode(Integer.MAX_VALUE, head); + ListNode preIndex = pre; + ListNode index = pre.next; + while (index!= null) { + if (index.next != null && index.next.val == index.val) { + while (index.next != null && index.next.val == index.val) { + index = index.next; + } + preIndex.next = index.next; + index = preIndex.next; + } else { + preIndex = index; + index = index.next; + } + } + return pre.next; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode84_1.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode84_1.java new file mode 100644 index 0000000..3f913ad --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode84_1.java @@ -0,0 +1,100 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/2 22:32 + * @注释 + */ +public class LeetCode84_1 { + + @Test + public void test() { + int[] heights = {2,1,5,6,2,3}; + System.out.println(new Solution().largestRectangleArea(heights)); + } + + class Solution { + public int largestRectangleArea(int[] heights) { + // 每个元素,找到左右第一个比他小的元素 + int[] leftMin = new int[heights.length]; + int[] rightMin = new int[heights.length]; + + leftMin[0] = -1; + for (int i = 1; i < heights.length; i++) { + int nowHei = heights[i]; + int index = i - 1; + while (index >= 0 && heights[index] >= nowHei) { + index = leftMin[index]; + } + leftMin[i] = index; + } + + rightMin[heights.length - 1] = heights.length; + for (int i = heights.length - 2; i >= 0; i--) { + int nowHei = heights[i]; + int index = i + 1; + while (index <= heights.length - 1 &&heights[index] >= nowHei) { + index = rightMin[index]; + } + rightMin[i] = index; + } + + int max = 0; + for (int i = 0; i < rightMin.length; i++) { + max = Math.max(max, heights[i] * (rightMin[i] - leftMin[i] - 1)); + } + return max; + + } + } + + class Solution1 { + /** + * 单调栈 + * + * 有凹凸, \_/这种 每次弹出就可以计算一次值 + * + * @param heights + * @return + */ + public int largestRectangleArea(int[] heights) { + + int[] ints = new int[heights.length + 2]; + System.arraycopy(heights, 0, ints, 1, heights.length); + heights = ints; + + Deque stack = new LinkedList<>(); + stack.push(0); + int max = 0; + for (int i = 1; i < heights.length; i++) { + int now = heights[i]; + if (heights[stack.peek()] > now) { + while (!stack.isEmpty() && now < heights[stack.peek()]) { + /** + * + * + * 2 3 1 + * + */ + Integer pop = stack.pop(); + if (!stack.isEmpty()) { + max = Math.max(max, (i - stack.peek() - 1) * heights[pop]); + } + } + + } + stack.push(i); + + } + + return max; + } + } +}