首页 > 其他分享 >(LeetCode 热题 100) 1143. 最长公共子序列(动态规划dp)

(LeetCode 热题 100) 1143. 最长公共子序列(动态规划dp)

时间:2024-10-09 15:47:45浏览次数:3  
标签:1143 int max LeetCode text2 text1 序列 热题 Math

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

在这里插入图片描述
在这里插入图片描述

思路:经典动态规划dp题型,时间复杂度为0(n^2)。

C++版本:

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int n=text1.size(),m=text2.size();
        //状态f[i][j]表示:text1[0,i]和text2[0,j]之间的最长公共子序列
        vector<vector<int>> f(n+1,vector<int>(m+1,0));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
            	//相等的话
                if(text1[i]==text2[j]){
                    f[i+1][j+1]=max(f[i+1][j+1],f[i][j]+1);
                }
                //不相等的话
                f[i+1][j+1]=max(f[i+1][j+1],f[i+1][j]);
                f[i+1][j+1]=max(f[i+1][j+1],f[i][j+1]);
            }
        }
        return f[n][m];
    }
};

JAVA 版本:

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int n=text1.length(),m=text2.length();
        //状态f[i][j]表示:text1[0,i]和text2[0,j]之间的最长公共子序列
        int[][] f =new int[n+1][m+1];
        /*
        for(var x:f){
            Arrays.fill(x,0);
        }
        */
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
            	//相等的话
                if(text1.charAt(i)==text2.charAt(j)){
                    f[i+1][j+1]=Math.max(f[i+1][j+1],f[i][j]+1);
                }
                //不相等的话
                f[i+1][j+1]=Math.max(f[i+1][j+1],f[i+1][j]);
                f[i+1][j+1]=Math.max(f[i+1][j+1],f[i][j+1]);
            }
        }
        return f[n][m];
    }
}

标签:1143,int,max,LeetCode,text2,text1,序列,热题,Math
From: https://blog.csdn.net/weixin_46028214/article/details/142788567

相关文章

  • Leetcode 18. 四数之和
    1.题目基本信息1.1.题目描述给你一个由n个整数组成的数组nums,和一个目标值target。请你找出并返回满足下述全部条件且不重复的四元组[nums[a],nums[b],nums[c],nums[d]](若两个四元组元素一一对应,则认为两个四元组重复):0<=a,b,c,d<na、b、c和d互不相同nu......
  • LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts
    原题链接在这里:https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/description/题目:Giventhestring s,returnthesizeofthelongestsubstringcontainingeachvowelanevennumberoftimes.Thatis,'a','e&......
  • LeetCode 11 Container with Most Water 解题思路和python代码
    题目:Youaregivenanintegerarrayheightoflengthn.Therearenverticallinesdrawnsuchthatthetwoendpointsoftheithlineare(i,0)and(i,height[i]).Findtwolinesthattogetherwiththex-axisformacontainer,suchthatthecontainerco......
  • LeetCode 209 Minimum Size Subarray Sum 题目解析和python代码
    题目:Givenanarrayofpositiveintegersnumsandapositiveintegertarget,returntheminimallengthofasubarraywhosesumisgreaterthanorequaltotarget.Ifthereisnosuchsubarray,return0instead.Example1:Input:target=7,nums=[2,3,......
  • leetcode 刷题day35动态规划Part04 背包问题(1049. 最后一块石头的重量 II 、494. 目标
    1049.最后一块石头的重量II思路:解题的难度在于如何转化为背包问题。本题的核心思路是让石头分成重量相同的两堆,相撞之后剩下的石头最小,这样就化解成01背包问题,和416.分割等和子集非常像了。首先,背包的容量target为sum/2向下取整,得到dp[target]就是分出来较小的一堆,用su......
  • leetcode 刷题day37动态规划Part06背包问题( 322. 零钱兑换、279.完全平方数、139.单词
    322.零钱兑换思路:每种硬币的数量是无限的,是典型的完全背包问题。但是题目要求等于目标值的最小硬币个数。所以这里需要对动规五部曲进行分析。动规五部曲:1、确定dp数组以及下标的含义dp[j]:凑足总额为j所需钱币的最少个数为dp[j]2、确定递推公式凑足总额为j-coins[i......
  • leetcode313. 超级丑数
    超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组 primes 中。给你一个整数 n 和一个整数数组 primes ,返回第 n 个 超级丑数 。题目数据保证第 n 个 超级丑数 在 32-bit 带符号整数范围内。示例1:输入:n=12,primes=[2,7,13,19]输出:32解......
  • leetcode926. 将字符串翻转到单调递增
    如果一个二进制字符串,是以一些 0(可能没有 0)后面跟着一些 1(也可能没有 1)的形式组成的,那么该字符串是 单调递增 的。给你一个二进制字符串 s,你可以将任何 0 翻转为 1 或者将 1 翻转为 0 。返回使 s 单调递增的最小翻转次数。示例1:输入:s="00110"输出:1......
  • Leetcode 37. 解数独
    1.题目基本信息1.1.题目描述编写一个程序,通过填充空格来解决数独问题。数独的解法需遵循如下规则:数字1-9在每一行只能出现一次。数字1-9在每一列只能出现一次。数字1-9在每一个以粗实线分隔的3×3宫内只能出现一次。(请参考示例图)数独部分空格内已填入了数字,空白......
  • Day 29 动态规划part02| LeetCode 62.不同路径,63.不同路径II
    62.不同路径62.不同路径classSolution{publicintuniquePaths(intm,intn){int[][]dp=newint[m][n];//dp数组--dp[i][j]达到坐标(i,j)有几种路径//dp数值初始化--终点为:dp[m-1][n-1]for......