LeetCode 1143最長共通部分(のJava版; Meidum)

私のブログへようこそ

LeetCode 1143最長共通部分(の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]は、I、Jの文字と2の文字シーケンスのText1とText2の前に最長共通正面を表す)、DP私は、私を意味するものの前に、1からnまでのループを開始するために、文字の文字列を取ることがあり、インデックスはI-1であるのではない使用して、ルーチンまたは固定的な思考に属しているとき、モード3)暴力再帰によると、あなたは心のDPでの2次元テーブルを想像することができます

ループ終了条件のための始まりは間違っています...

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];
    }
}

初めて、暴力再帰;タイムアウト37分の11コア:1)文字列にも2つのポインタ、どのようにポインタを移動させます

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);
        }
    }
}
公開された603元の記事 ウォンの賞賛148 ビュー210 000 +

おすすめ

転載: blog.csdn.net/littlehaes/article/details/104882306