力扣爆刷第79天--动态规划一网打尽子序列一维二维连续不连续问题

力扣爆刷第79天–动态规划一网打尽子序列一维二维连续不连续问题

零、总结

今天的专题是子序列问题,有一维的,也有二维的,有求连续的,也有求不连续的,组合是四种类型,且看一网打尽。

一、300.最长递增子序列

题目链接:https://leetcode.cn/problems/longest-increasing-subsequence/
思路:求最长递增子序列,定义dp[i]表示在区间[0,i]种,以nums[i]为结尾的最长递增子序列的长度,确定了dp[i]的定义以后,dp[i]就好推导了,既然dp[i]是以nums[i]为结尾的,那么自然要从[0,i-1]找小于nums[i]的值,遍历一遍,然后更新dp[i]。

class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        int max = 1;
        for(int i = 1; i < nums.length; i++) {
            for(int j = 0; j < i; j++) {
                if(nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j]+1);
                }
            }
            max = Math.max(max, dp[i]);
        }
        return max;
    }
}

二、674. 最长连续递增序列

题目链接:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/description/
思路:连续的话就更好做了,都不需要在小区间里再遍历,上一题是非连续,需要在[0, i-1]区间里遍历,本题连续,连续就加1,不连续就为1,非常简单。

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

三、718. 最长重复子数组

题目链接:https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
思路:本题求的是最长连续重复子数组,因为是重复子数组,数组是连续的,如果是最长重复子序列,那么就是不连续的,要求两个数组有最长的,连续重复部分,那么定义dp[i][j]表示以nums1[0, i]和nums2[0, j]中以nums1[i]和nums2[j]为结尾的最长连续重复子数组的长度,那么只要nums1[i]==nums2[j]可以得到dp[i][j]=dp[i-1][j-1]+1,
如果nums1[i] != nums2[j],那么dp[i][j]=0。

class Solution {
     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;
                }
                max = Math.max(dp[i+1][j+1], max);
            }
        }
        return max;
    }
}

四、1143.最长公共子序列

题目链接:https://leetcode.cn/problems/longest-common-subsequence/
思路:本题正好和上一题是一对,上一题求的是两个数组的最长重复子数组,求的是连续的重复,本题求的是最长公共子序列,那自然求的是不要求连线的,定义和之前一样,dp[i][j]表示以nums1[0, i]和nums2[0, j]中以nums1[i]和nums2[j]为结尾的最长公共子序列的长度,
如果nums1[i] == nums2[j],那么dp[i][j]=dp[i-1][j-1]+1;
如果nums1[i] != nums2[j],那么dp[i][j]的长度就要延续之前最长的长度,如 1 2 3与1 2 4, 3与4不相等,那么便可以延续集和{1, 2, 3}和集合{1, 2}或者{1, 2}和{1, 2, 4},那么自然是包括dp[1][1]的,所以为dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);

class Solution {
     public int longestCommonSubsequence(String text1, String text2) {
        int[][] dp = new int[text1.length()+1][text2.length()+1];
        for(int i = 1; i <= text1.length(); i++) {
            for(int j = 1; j <= text2.length(); 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[text1.length()][text2.length()];
    }
}
  • 38
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
判断一个链表是否是另一个链表的子序列是一道常见的问题。对于这个问题,我们可以使用双指针的方法来解决。双指针一个指向主链表,一个指向子序列链表。我们同时遍历两个链表,比较指针指向的节点是否相同。如果相同,我们就同时向后移动两个指针;如果不相同,我们只移动主链表的指针。当子序列链表遍历完毕时,说明所有的节点都匹配成功,那么它是主链表的子序列;如果主链表遍历完毕,而子序列链表还没有遍历完,说明子序列链表中的节点没有完全匹配,那么它不是主链表的子序列。这种方法的时间复杂度是O(n + m),其中n是主链表的长度,m是子序列链表的长度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [力扣之判断一个链表是否是回文链表](https://blog.csdn.net/chenbaifan/article/details/121450273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [[力扣] 203.移除链表元素](https://download.csdn.net/download/weixin_38667920/13759251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年拼却醉颜红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值