算法笔记|Day38动态规划XI

☆☆☆☆☆leetcode 1143.最长公共子序列

题目链接:leetcode 1143.最长公共子序列

题目分析

首先将text1和text2转为char数组char1[]和char2[]。
1.dp数组含义:dp[i][j]表示以char1[i-1]和char2[j-1]之前的最长公共子序列长度;
2.递推公式:if(char1[i-1]==char2[j-1])dp[i][j]=dp[i-1][j-1]+1(如果前一个元素相等,dp[i][j]在dp[i-1][j-1]的基础上加一),其他情况dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1])(如果前一个元素不等,dp[i][j]取dp[i-1][j]和dp[i][j-1]的最大值)
3.初始化:所有dp[i][0]=0,所有dp[0][j]=0;
4.遍历顺序:从小到大。

代码

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        char char1[]=text1.toCharArray();
        char char2[]=text2.toCharArray();
        int dp[][]=new int[char1.length+1][char2.length+1];
        for(int i=1;i<=char1.length;i++){
            for(int j=1;j<=char2.length;j++){
                if(char1[i-1]==char2[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[char1.length][char2.length];
    }
}

☆☆☆☆☆leetcode 1035.不相交的线

题目链接:leetcode 1035.不相交的线

题目分析

不相交的线的最大条数即为两个数组最长公共子序列的长度。
1.dp数组含义:dp[i][j]表示以nums1[i-1]和nums2[j-1]为结尾的最长重复子数组长度,取所有dp[i][j]中的最大值即为所求最长重复子数组的长度;
2.递推公式:if(nums1[i-1]==nums2[j-1])dp[i][j]=dp[i-1][j-1]+1(如果结尾元素相等,dp[i][j]在dp[i-1][j-1]的基础上加一);
3.初始化:所有dp[i][0]=0,所有dp[0][j]=0;
4.遍历顺序:从小到大。

代码

class Solution {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int dp[][]=new int[nums1.length+1][nums2.length+1];
        for(int i=1;i<=nums1.length;i++){
            for(int j=1;j<=nums2.length;j++){
                if(nums1[i-1]==nums2[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[nums1.length][nums2.length];
    }
}

☆☆☆☆☆leetcode 53. 最大子序和

题目链接:leetcode 53. 最大子序和

题目分析

1.dp数组含义:dp[i]表示以nums[i]为结尾的最大连续子序列和为dp[i],取所有dp[i]中的最大值即为所求最大子序和;
2.递推公式:dp[i]=Math.max(dp[i-1]+nums[i],nums[i])(nums[i]加入当前连续子序列和即为dp[i-1]+nums[i],nums[i]不加入当前连续子序列和,也就是从头开始计算当前连续子序列和,即为nums[i]);
3.初始化:dp[0]=nums[0];
4.遍历顺序:从小到大。

代码

class Solution {
    public int maxSubArray(int[] nums) {
        int dp[]=new int[nums.length];
        dp[0]=nums[0];
        int res=dp[0];
        for(int i=1;i<nums.length;i++){
            dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
            res=Math.max(dp[i],res);
        }
        return res;
    }
}

☆☆☆☆☆leetcode 392.判断子序列

题目链接:leetcode 392.判断子序列

题目分析

首先将s和t转为char数组char1[]和char2[],s是否为t的子序列即为s与t的最长公共子序列长度是否等于s 的长度。
1.dp数组含义:dp[i][j]表示以char1[i-1]和char2[j-1]之前的最长公共子序列长度;
2.递推公式:if(char1[i-1]==char2[j-1])dp[i][j]=dp[i-1][j-1]+1(如果前一个元素相等,dp[i][j]在dp[i-1][j-1]的基础上加一),其他情况dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1])(如果前一个元素不等,dp[i][j]取dp[i-1][j]和dp[i][j-1]的最大值)
3.初始化:所有dp[i][0]=0,所有dp[0][j]=0;
4.遍历顺序:从小到大。

代码

class Solution {
    public boolean isSubsequence(String s, String t) {
        char char1[]=s.toCharArray();
        char char2[]=t.toCharArray();
        int dp[][]=new int[char1.length+1][char2.length+1];
        for(int i=1;i<=char1.length;i++){
            for(int j=1;j<=char2.length;j++){
                if(char1[i-1]==char2[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[char1.length][char2.length]==char1.length;
    }
}
  • 21
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值