LeetCode 1143. Longest Common Subsequence (Java版; Meidum)

welcome to my blog

LeetCode 1143. Longest Common Subsequence (Java版; Meidum)

题目描述
Given two strings text1 and text2, return the length of their longest common subsequence.

A subsequence of a string is a new string generated from the original string with some characters(can be
none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence
of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.

 

If there is no common subsequence, return 0.

 

Example 1:

Input: text1 = "abcde", text2 = "ace" 
Output: 3  
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:

Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:

Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
 

Constraints:

1 <= text1.length <= 1000
1 <= text2.length <= 1000
The input strings consist of lowercase English characters only.
第一次做; 暴力递归改动态规划; 核心: 1) 二维dp, dp[i][j]表示text1的前i个字符和text2的前j个字符的最长公共子序列 2) 当dp采用前i个什么什么的含义时, for循环从1开始, 并且直到n, 还有就是取字符串中的字符时, 索引是i-1, 并不是i, 这属于套路了或者说固定的思考模式3) 根据暴力递归, 可以在脑海中想象出二维的dp表

最开始for循环的终止条件写错了…

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        if(text1==null || text1.length()==0 || text2==null || text2.length()==0){
            return 0;
        }
        
        int n = text1.length(), m = text2.length();
        int[][] dp = new int[n+1][m+1];
        for(int i=1; i<=n; i++){
            char t1 = text1.charAt(i-1);
            for(int j=1; j<=m; j++){
                char t2 = text2.charAt(j-1);
                if(t1==t2){
                    //1表示当前的字符占据一个长度
                    dp[i][j] = 1 + dp[i-1][j-1];
                }
                else{
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[n][m];
    }
}
第一次做; 暴力递归; 超时11/37 核心: 1) 两个指针指向连个字符串, 如何移动指针
class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        if(text1==null || text1.length()==0 || text2==null || text2.length()==0){
            return 0;
        }
        return core(text1, text2, 0, 0);
    }

    //递归逻辑: t1==t2, 长度加一, 两个字符串同时继续向下判断; t1!=t2, 获取两个结果, 一个是text1向下移动,text2不动的结果, 另一个是text1不动, text2向下移动的结果, 返回这两个结果中更大的那个
    private int core(String text1, String text2, int i, int j){
        //base case
        if(i==text1.length() || j==text2.length()){
            return 0;
        }
        //
        char t1 = text1.charAt(i), t2 = text2.charAt(j);
        if(t1==t2){
            return 1 + core(text1, text2, i+1, j+1);
        }
        else{
            int res1 = core(text1, text2, i+1, j);
            int res2 = core(text1, text2, i, j+1);
            return Math.max(res1, res2);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值