refactor(interview): 重命名ccn包为zhaoyun包

将'cn.whaifree.interview.ccn'包重命名为'cn.whaifree.interview.zhaoyun',以反映新的命名约定。c2.java和p2.java文件中的包声明已更新。

feat(redo): 添加LeetCode718和LeetCode1143题目解决方案

新增'LeetCode718'和'LeetCode1143'两个类,分别提供'最长公共子数组'和'最长公共子序列'问题的解决方案。包括单元测试和算法实现。
This commit is contained in:
whaifree 2024-09-12 01:05:01 +08:00
parent 597ef47dc0
commit b96497213a
4 changed files with 99 additions and 2 deletions

View File

@ -1,4 +1,4 @@
package cn.whaifree.interview.ccn.p1; package cn.whaifree.interview.zhaoyun.p1;
/** /**
* @version 1.0 * @version 1.0
@ -7,4 +7,7 @@ package cn.whaifree.interview.ccn.p1;
* @注释 * @注释
*/ */
public class c2 { public class c2 {
} }

View File

@ -1,4 +1,4 @@
package cn.whaifree.interview.ccn; package cn.whaifree.interview.zhaoyun;
import org.junit.Test; import org.junit.Test;

View File

@ -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];
}
}
}

View File

@ -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-1j-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;
}
}
}