From b96497213a52b29ed780141cbf98341e5f800495 Mon Sep 17 00:00:00 2001 From: whaifree Date: Thu, 12 Sep 2024 01:05:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(interview):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8Dccn=E5=8C=85=E4=B8=BAzhaoyun=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将'cn.whaifree.interview.ccn'包重命名为'cn.whaifree.interview.zhaoyun',以反映新的命名约定。c2.java和p2.java文件中的包声明已更新。 feat(redo): 添加LeetCode718和LeetCode1143题目解决方案 新增'LeetCode718'和'LeetCode1143'两个类,分别提供'最长公共子数组'和'最长公共子序列'问题的解决方案。包括单元测试和算法实现。 --- .../interview/{ccn => zhaoyun}/p1/c2.java | 5 +- .../interview/{ccn => zhaoyun}/p2.java | 2 +- .../redo/redo_all_240721/LeetCode1143.java | 43 ++++++++++++++++ .../redo/redo_all_240721/LeetCode718.java | 51 +++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) rename src/main/java/cn/whaifree/interview/{ccn => zhaoyun}/p1/c2.java (69%) rename src/main/java/cn/whaifree/interview/{ccn => zhaoyun}/p2.java (98%) create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode1143.java create mode 100644 src/main/java/cn/whaifree/redo/redo_all_240721/LeetCode718.java 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; + } + } +}