dp4-力扣-java-最长递增子序列+ 最长公共子序列

本文深入解析了两道动态规划的经典题目:最长递增子序列和最长公共子序列。通过详细的代码实现,展示了如何利用动态规划解决这类问题,为读者提供了理解和掌握动态规划算法的实践案例。

class Solution {
    public int lengthOfLIS(int[] nums) {
        if(nums.length<2) return nums.length;
        int n=nums.length;
        int res=0;
        int []dp=new int[n];//dp[i]表示以nums[i]结尾的「上升子序列」的长度
        Arrays.fill(dp,1);
        for(int i=0;i<n;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;
    }
}
//https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/dong-tai-gui-hua-er-fen-cha-zhao-tan-xin-suan-fa-p/

 

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int m=text1.length(),n=text2.length();
        //dp[i][j] 表示text1[0~i-1]和text2[0~j-1]的最长公共子序列长度
        int [][]dp=new int[m+1][n+1];
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(text1.charAt(i-1)==text2.charAt(j-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[m][n];
    }
}

 

### 题目描述 给定两个字符串 `text1` 和 `text2`,返回这两个字符串的最长公共子序列的长度。如果不存在公共子序列,返回 0。一个字符串的子序列是指由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。两个字符串的公共子序列是这两个字符串所共同拥有的子序列。例如,输入 `text1 = “abcde”`,`text2 = “ace”`,输出为 3,因为最长公共子序列是 “ace”,它的长度为 3[^1][^4]。 ### 解题思路 - **状态定义**:`dp[i][j]` 中 `i` 表示字符串 1 的前 `i` 个字符,`j` 表示字符串 2 的前 `j` 个字符,`dp[i][j]` 表示 `s1[0…i]` 和 `s2[0…j]` 的最长公共子序列。`dp` 数组大小为 `(len1 + 1) * (len2 + 1)`,其中 `len1` 和 `len2` 分别是两个字符串的长度[^1]。 - **状态转移方程**: - 若 `s1[i - 1] == s2[j - 1]`,则该字符需要添加到最长公共子序列中,`dp[i][j] = dp[i - 1][j - 1] + 1`。 - 若 `s1[i - 1] != s2[j - 1]`,则 `dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])`[^1][^3][^5]。 - **初始化**:`dp[0][j]` 和 `dp[i][0]` 都表示空字符串与字符串 `s` 的公共序列,长度设置为 0[^1]。 - **输出**:`dp[len1][len2]` 即为两个字符串的最长公共子序列的长度[^1][^3][^5]。 ### 代码实现 #### C++ 实现 ```cpp class Solution { public: int longestCommonSubsequence(string text1, string text2) { int len1 = text1.size(); int len2 = text2.size(); if(len1 == 0 || len2 == 0) return 0; int dp[len1 + 1][len2 + 1]; memset(dp, 0, sizeof(dp)); for(int i = 1; i <= len1; i++){ for(int j = 1; j <= len2; j++){ if(text1[i - 1] == text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return dp[len1][len2]; } }; ``` #### Java 实现 ```java import java.util.Arrays; class Solution { public int longestCommonSubsequence(String text1, String text2) { int rows = text1.length() + 1; int cols = text2.length() + 1; int[][] dp = new int[rows][cols]; for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { if (text1.charAt(i - 1) == text2.charAt(j - 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[rows - 1][cols - 1]; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值