diff --git a/src/main/java/cn/whaifree/interview/ccn/p1/c2.java b/src/main/java/cn/whaifree/interview/zhaoyun/p1/c2.java similarity index 69% rename from src/main/java/cn/whaifree/interview/ccn/p1/c2.java rename to src/main/java/cn/whaifree/interview/zhaoyun/p1/c2.java index f9053f8..9863830 100644 --- a/src/main/java/cn/whaifree/interview/ccn/p1/c2.java +++ b/src/main/java/cn/whaifree/interview/zhaoyun/p1/c2.java @@ -1,4 +1,4 @@ -package cn.whaifree.interview.ccn.p1; +package cn.whaifree.interview.zhaoyun.p1; /** * @version 1.0 @@ -7,4 +7,7 @@ package cn.whaifree.interview.ccn.p1; * @注释 */ public class c2 { + + + } diff --git a/src/main/java/cn/whaifree/interview/ccn/p2.java b/src/main/java/cn/whaifree/interview/zhaoyun/p2.java similarity index 98% rename from src/main/java/cn/whaifree/interview/ccn/p2.java rename to src/main/java/cn/whaifree/interview/zhaoyun/p2.java index 9857525..a78cfa5 100644 --- a/src/main/java/cn/whaifree/interview/ccn/p2.java +++ b/src/main/java/cn/whaifree/interview/zhaoyun/p2.java @@ -1,4 +1,4 @@ -package cn.whaifree.interview.ccn; +package cn.whaifree.interview.zhaoyun; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1143.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1143.java new file mode 100644 index 0000000..e2a554c --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1143.java @@ -0,0 +1,43 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/12 0:53 + * @注释 + */ +public class LeetCode1143 { + + @Test + public void test() { + System.out.println(new Solution().longestCommonSubsequence("ddd", "ace")); + } + + class Solution { + /** + * + * + * @param text1 + * @param text2 + * @return + */ + public int longestCommonSubsequence(String text1, String text2) { + int len1 = text1.length(); + int len2 = text2.length(); + int[][] dp = new int[len1 + 1][len2 + 1]; + for (int i = 0; i < len1; i++) { + for (int j = 0; j < len2; j++) { + if (text1.charAt(i) == text2.charAt(j)) { + dp[i + 1][j + 1] = dp[i][j] + 1; + }else { + dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]); + } + + } + } + return dp[len1][len2]; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode718.java b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode718.java new file mode 100644 index 0000000..f4e1673 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode718.java @@ -0,0 +1,51 @@ +package cn.whaifree.redo.redo_all_240721; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/9/12 0:37 + * @注释 + */ +public class LeetCode718 { + + @Test + public void test() { + System.out.println(new Solution().findLength(new int[]{3,2,1,4,7},new int[]{1,2,3,2,1})); + } + + + class Solution { + /** + * dp[i][j] 表示 i-1,j-1为结尾的最长字数组长度 + * + * 1 2 3 2 1 + * + * 3 0 0 1 1 1 + * 2 0 1 1 2 2 max上左边 相等取i-1 j-1 +1 + * 1 + * 4 + * 7 + * + * @param nums1 + * @param nums2 + * @return + */ + public int findLength(int[] nums1, int[] nums2) { + int[][] dp = new int[nums1.length+1][nums2.length+1]; + int max = 0; + for (int i = 0; i < nums1.length; i++) { + for (int j = 0; j < nums2.length; j++) { + if (nums1[i] == nums2[j]) { + dp[i + 1][j + 1] = dp[i][j] + 1; + }else { + dp[i + 1][j + 1] = 0; + } + max = Math.max(max, dp[i + 1][j + 1]); + } + } + return max; + } + } +}