成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode] Edit Distance 最小編輯距離

zhangke3016 / 3183人閱讀

摘要:動態(tài)規(guī)劃復(fù)雜度時間空間思路這是算法導(dǎo)論中經(jīng)典的一道動態(tài)規(guī)劃的題。

Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

動態(tài)規(guī)劃 復(fù)雜度

時間 O(NM) 空間 O(NM)

思路

這是算法導(dǎo)論中經(jīng)典的一道動態(tài)規(guī)劃的題。假設(shè)dp[i-1][j-1]表示一個長為i-1的字符串str1變?yōu)殚L為j-1的字符串str2的最短距離,如果我們此時想要把str1a這個字符串變成str2b這個字符串,我們有如下幾種選擇:

替換: 在str1變成str2的步驟后,我們將str1a中的a替換為b,就得到str2b (如果ab相等,就不用操作)

增加: 在str1a變成str2的步驟后,我們再在末尾添加一個b,就得到str2b (str1a先根據(jù)已知距離變成str2,再加個b)

刪除: 在str1變成str2b的步驟后,對于str1a,我們將末尾的a刪去,就得到str2b (str1aa刪去得到str1,而str1str2b的編輯距離已知)

根據(jù)這三種操作,我們可以得到遞推式
若a和b相等:

dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i][j])

若a和b不相等:

dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i][j]+1)

因為將一個非空字符串變成空字符串的最小操作數(shù)是字母個數(shù)(全刪),反之亦然,所以:

dp[0][j]=j, dp[i][0]=i

最后我們只要返回dp[m][n]即可,其中m是word1的長度,n是word2的長度

詳解請看斯坦福課件

代碼
public class Solution {
    public int minDistance(String word1, String word2) {
        int m = word1.length(), n = word2.length();
        int[][] dp = new int[m + 1][n + 1];
        // 初始化空字符串的情況
        for(int i = 1; i <= m; i++){
            dp[i][0] = i;
        }
        for(int i = 1; i <= n; i++){
            dp[0][i] = i;
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                // 增加操作:str1a變成str2后再加上b,得到str2b
                int insertion = dp[i][j-1] + 1;
                // 刪除操作:str1a刪除a后,再由str1變?yōu)閟tr2b
                int deletion = dp[i-1][j] + 1;
                // 替換操作:先由str1變?yōu)閟tr2,然后str1a的a替換為b,得到str2b
                int replace = dp[i-1][j-1] + (word1.charAt(i - 1) == word2.charAt(j - 1) ? 0 : 1);
                // 三者取最小
                dp[i][j] = Math.min(replace, Math.min(insertion, deletion));
            }
        }
        return dp[m][n];
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/64502.html

相關(guān)文章

  • [Leetcode] One Edit Distance 編輯距離為一

    摘要:比較長度法復(fù)雜度時間空間思路雖然我們可以用的解法,看是否為,但中會超時。這里我們可以利用只有一個不同的特點在時間內(nèi)完成。 One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 比較長度法 復(fù)雜度 時間 O(N) 空間 O(1) 思路 雖然我們可以用...

    lewinlee 評論0 收藏0
  • Leetcode[161] One Edit Distance

    摘要:復(fù)雜度思路考慮如果兩個字符串的長度,是肯定當(dāng)兩個字符串中有不同的字符出現(xiàn)的時候,說明之后的字符串一定要相等。的長度比較大的時候,說明的時候,才能保證距離為。 LeetCode[161] One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. Stri...

    周國輝 評論0 收藏0
  • leetcode72. Edit Distance

    摘要:題目要求輸入兩個字符串和,允許對進行插入,刪除和替換的操作,計算出將轉(zhuǎn)化為所需要的最少的操作數(shù)。其中存儲的是轉(zhuǎn)換為的最小步數(shù)。首先從邊緣情況開始考慮。只要在此基礎(chǔ)上再進行一次插入操作即可以完成轉(zhuǎn)換。 題目要求 Given two words word1 and word2, find the minimum number of steps required to convert wor...

    venmos 評論0 收藏0
  • [LintCode/LeetCode] Edit Distance

    摘要:構(gòu)造數(shù)組,是的,是的,是將位的轉(zhuǎn)換成位的需要的步數(shù)。初始化和為到它們各自的距離,然后兩次循環(huán)和即可。 Problem Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 s...

    snowell 評論0 收藏0
  • 文本相似度 余弦值相似度算法 VS L氏編輯距離(動態(tài)規(guī)劃)

    摘要:本文對兩種文本相似度算法進行比較。余弦值相似度算法最小編輯距離法氏編輯距離基于詞條空間編輯距離,又稱距離,是指兩個字串之間,由一個轉(zhuǎn)成另一個所需的最少編輯操作次數(shù)。但是同時也可以看出余弦相似度得到的結(jié)果相對比較高一些。 本文由作者祝娜授權(quán)網(wǎng)易云社區(qū)發(fā)布。 本文對兩種文本相似度算法進行比較。余弦值相似度算法 VS 最小編輯距離法1、L氏編輯距離(基于詞條空間)編輯距離(Edit Dist...

    fxp 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<