From 6bddb836c9165a8b0d93eb36503196694fc227bf Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Thu, 25 Apr 2024 13:16:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E6=96=B9=E6=A1=88=EF=BC=9A=E4=B8=8D=E7=9B=B8=E4=BA=A4=E7=BA=BF?= =?UTF-8?q?=E3=80=81=E6=9C=80=E5=A4=A7=E5=AD=90=E5=BA=8F=E5=88=97=E5=92=8C?= =?UTF-8?q?=E3=80=81=E5=AD=90=E5=BA=8F=E5=88=97=E5=88=A4=E6=96=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/Dynamic/LeetCode1035.java | 61 ++++++++++++++ .../leetCode/Dynamic/LeetCode392.java | 82 +++++++++++++++++++ .../whaifree/leetCode/Dynamic/LeetCode53.java | 47 +++++++++++ 3 files changed, 190 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode1035.java create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode392.java create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode53.java diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode1035.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode1035.java new file mode 100644 index 0000000..b0074d3 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode1035.java @@ -0,0 +1,61 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/25 11:28 + * @注释 + */ +public class LeetCode1035 { + + @Test + public void test() + { + int[] nums1 = new int[]{1,3,7,1,7,5}; + int[] nums2 = new int[]{1,9,2,5,1}; + System.out.println(new Solution().maxUncrossedLines(nums1, nums2)); + } + + class Solution { + /** + * 不相交,就是挨个求nums1的子序列在nums中出现的次数 + * - 不用连续 + * - 子序列 + * + * dp[i][j] 表示 nums1从0-i-1,nums2从0-j-1,有几个相同的子序列 + * + * if nums1[i] == nums2[j] + * dp[i][j] = dp[i-1][j-1]+1 + * else + * dp[i][j] = dp[i-1][j-1] + * + * + * @param nums1 短的 + * @param nums2 长的 + * @return + */ + public int maxUncrossedLines(int[] nums1, int[] nums2) { + if (nums1.length > nums2.length) { + return maxUncrossedLines(nums2, nums1); + } + int[][] dp = new int[nums1.length + 1][nums2.length + 1]; + + for (int i = 1; i <= nums1.length; i++) { + for (int j = 1; j <= nums2.length; j++) { + if (nums1[i - 1] == nums2[j - 1]) { + // 对应位置相等,则从i-1 j-1的值+1 + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + // 对应的值不相等,则获取前面已经匹配的最大值 + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[nums1.length][nums2.length]; + } + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode392.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode392.java new file mode 100644 index 0000000..ce37a13 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode392.java @@ -0,0 +1,82 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/25 12:41 + * @注释 + */ +public class LeetCode392 { + @Test + public void test() { + String s = "abc"; + String t = "ahbgdc"; + boolean subsequence = new Solution1().isSubsequence(s, t); + System.out.println(subsequence); + } + + class Solution { + /** + * 子序列 + * s是否是t的子序列 + * + * dp[i][j] 表示 + * + * @param s + * @param t + * @return + */ + public boolean isSubsequence(String s, String t) { + + boolean flag = false; + int j = 0; + for (int i = 0; i < s.length(); i++) { + while (j < t.length()) { + if (s.charAt(i) == t.charAt(j)) { + flag = true; + j++; + break; + } + j++; + } + if (!flag) { + return false; + } + flag = false; + } + return true; + } + } + + class Solution1 { + /** + * dp[i][j]表示s的i到t的j 相同子序列的长度 + * + * 转变为 求两个序列相同子序列的长度 + * + * ahbgdc + * acbdc + * @param s + * @param t + * @return + */ + public boolean isSubsequence(String s, String t) { + + int[][] dp = new int[s.length() + 1][t.length() + 1]; + char[] s1 = s.toCharArray(); + char[] t1 = t.toCharArray(); + for (int i = 1; i <= s1.length; i++) { + for (int j = 1; j <= t1.length; j++) { + if (s1[i - 1] == t1[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]); + } + } + } + return dp[s1.length][t1.length] == s1.length; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode53.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode53.java new file mode 100644 index 0000000..70d280a --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode53.java @@ -0,0 +1,47 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/25 11:59 + * @注释 + */ +public class LeetCode53 { + + @Test + public void test() { + Solution solution = new Solution(); + int[] nums = new int[]{-1,-3}; + int i = solution.maxSubArray(nums); + System.out.println(i); + } + + class Solution { + + /** + * + * @param nums + * @return + */ + public int maxSubArray(int[] nums) { + // dp[i] 表示包含元素i的的最大和 + // dp[0] = nums[0] + + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + int max = nums[0]; + for (int i = 1; i < nums.length; i++) { + if (dp[i - 1] > 0 && dp[i - 1] + nums[i] > 0) { // 如果p[i - 1] < 0 则只能拖后腿 + dp[i] = dp[i - 1] + nums[i]; + } else { + // 拖后腿,重新计算 + dp[i] = nums[i]; + } + max = Math.max(max, dp[i]); + } + return max; + } + } +}