From 121028b95585231a8c6fd077b0cbe6062008a934 Mon Sep 17 00:00:00 2001 From: whaifree <49432110+whaibetter@users.noreply.github.com> Date: Tue, 30 Apr 2024 12:55:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86LeetCode300=E5=92=8C?= =?UTF-8?q?LeetCode115=E7=9A=84=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86=E6=8F=90=E4=BA=A4=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=A0=BC=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redo/redo_24_4_27/LeetCode115.java | 65 +++++++++++++++++++ .../redo/redo_24_4_27/LeetCode300.java | 52 +++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode115.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode300.java diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode115.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode115.java new file mode 100644 index 0000000..86450a7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode115.java @@ -0,0 +1,65 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/30 11:20 + * @注释 + */ +public class LeetCode115 { + @Test + public void test() { + String s = "babgbag"; + String t = "bag"; + System.out.println(new Solution().numDistinct(s, t)); + } + + + + class Solution { + /** + * 给你两个字符串 s 和 t ,统计并返回在 s 的 子序列 中 t 出现的个数,结果需要对 109 + 7 取模。 + * @param s + * @param t + * @return + */ + public int numDistinct(String s, String t) { + /** + * + * dp[i][j] 表示0-j在0-i子序列中出现的次数 + * '' b a b g b a g + *'' 1 1 1 1 1 1 1 1 + * b 0 1 1 2 2 3 3 3 + * a 0 0 1 1 1 1 4 4 + * g 0 0 0 0 1 1 1 5 + * + * if s[i] == t[j] + * dp[i][j] = + * + + * - s使用i-1个位置 dp[i-1][j-1] + * - s不使用i-1个位置 dp[i][j-1] + * else + * dp[i][j-1] + */ + + int lenS = s.length(); + int lenT = t.length(); + int[][] dp = new int[lenT + 1][lenS + 1]; + for (int i = 0; i <= lenS; i++) { + dp[0][i] = 1; + } + for (int j = 1; j <= lenT; j++) { + for (int i = 1; i <= lenS; i++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[j][i] = dp[j - 1][i - 1] + dp[j][i - 1]; + } else { + dp[j][i] = dp[j][i - 1]; + } + } + } + return dp[lenT][lenS]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode300.java b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode300.java new file mode 100644 index 0000000..776373b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_4_27/LeetCode300.java @@ -0,0 +1,52 @@ +package cn.whaifree.redo.redo_24_4_27; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/4/30 11:49 + * @注释 + */ +public class LeetCode300 { + @Test + public void test() + { + Solution solution = new Solution(); + int[] nums = new int[]{10, 9, 2, 5, 3, 7, 101, 18}; + int res = solution.lengthOfLIS(nums); + System.out.println(res); + } + + class Solution { + /** + * 最长严格递增子序列的长度。 + * + * dp[i]表示以i为结尾的最长递增子序列的长度 (计算i的时候,0~(i-1)已经满足最长递增子序列的长度) + * + * + * if (nums[j] < nums[i]) + * dp[i] = dp[j] + 1 + * dp[j] = dp[i] + * + * @param nums + * @return + */ + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + int res = 1; + for (int i = 1; i < nums.length; i++) { + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + res = Math.max(res, dp[i]); + } + return res; + } + } +}