1. 程式人生 > >LeetCode 72. Edit Distance

LeetCode 72. 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

分析:假設dp[i][j]表示word1[0…i]和word2[0…j]的最小編輯距離。
如果word1為空,則刪除word2中元素
如果word2為空,則刪除word1中元素
都不為空,有三種選擇:
1)刪除word1的最後一個字元,此時dp[i-1][j] + 1
2) 刪除word2的最後一個字元,此時dp[i][j-1] + 1
3) word1最後一個替換為word2最後一個 , 則dp[i-1][j-1] + (word1[i] != word2[j])(下標從1開始)

程式碼如下:

class Solution {
public:
    int minDistance(string word1,string word2)
    {
        int m = word1.length();
        int n = word2.length();
        int dp[m+1][n+1];
        memset(dp,0,sizeof(dp));

        for(int i = 0; i <= m; i++) dp[i][0] = i;
        for(int i = 0; i <= n; i++) dp[0
][i] = i; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { int num ; if(word1[i-1] == word2[j-1]) num = 0; else num = 1; dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1] + 1, dp[i-1
][j-1] + num) ); } } return dp[m][n]; } };

相關推薦

第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

blog http else true rep 猜想 col () 字符串 Leetcode72 看起來比較棘手的一道題(列DP方程還是要大膽猜想。。) DP方程該怎麽列呢? dp[i][j]表示字符串a[0....i-1]轉化為b[0....j-1]的最少距離 轉移方程分

leetcode 72. Edit Distance

沒有 require targe 了吧 length rip follow des steps link Given two words word1 and word2, find the minimum number of steps required to conv

[leetcode]161. One Edit Distance編輯步數為一 [leetcode]72. Edit Distance 最少編輯步數

Given two strings s and t, determine if they are both one edit distance apart. Note:  There are 3 possiblities to satisify one edit d

LeetCode 72-Edit Distance(動態規劃)

本題為經典的動態規劃。解決本題的前提是把這題的動態方程先搞明白。 題目:(動態規劃)https://leetcode.com/problems/edit-distance/ 概述:給定兩個單詞,給了三種方法(替換,刪除,插入),用這三種方法使得兩個單詞相同。 思路:採用動態規

LeetCode 72.Edit Distance (編輯距離)

題目: 給定兩個單詞 word1 和 word2,計算出將 word1 轉換成 word2 所使用的最少運算元 。 你可以對一個單詞進行如下三種操作: 插入一個字元 刪除一個字元 替換一個字

[leetcode]72. Edit Distance

沒想出來。不是dp難,是不會抽象這個關係 class Solution { public int minDistance(String word1, String word2) { int n = word1.length(); int m = word2.lengt

leetCode 72.Edit Distance (編輯距離) 解題思路和方法

Edit Distance Given two words word1 and word2, find the minimum number of steps required to conve

DP動態規劃專題十三:LeetCode 72. Edit Distance

LeetCode 72. Edit Distance Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have

DP動態規劃專題九:LeetCode 72. Edit Distance

LeetCode 72. Edit Distance Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have

LeetCode 72. 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

**Leetcode 72. 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

leetcode-72. Edit Distance

問題描述:Word1要經過多少步操作才能變成Word2操作:增加一個字元;刪除一個字元;替換一個字元思路:設dp[i][j]為將Word1前i個字元轉換成Word2前j個字元所需操作步數,則問題轉換為求dp[len1][len2]dp[i][j]可以由以下幾種路徑得出:1.

LeetCode 72 Edit Distance(Python詳解及實現)

【題目】 Given two words word1 and word2, find theminimum number of steps required to convert word1 to word2. (each operation iscounted as 1

19.2.13 [LeetCode 72] Edit Distance

gif execution b- lse output splay 操作 夠快 hellip Given two words word1 and word2, find the minimum number of operations required to convert

LeetCode-72-Edit Distance

算法 min ring lee character ++ edit operation rmi 算法描述: Given two words word1 and word2, find the minimum number of operations required to

[LeetCode] 72. Edit Distance 編輯距離 @python

Description Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have

LeetCode-72.Edit Distance

lac length words pre turn 前行 ret cte etc Given two words word1 and word2, find the minimum number of operations required to convert word1

Leetcode】【DP】 72. Edit Distance / 編輯距離

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have th

LeetCode72. Edit Distance(C++)

地址:https://leetcode.com/problems/edit-distance/ 題目: Given two words word1 and word2, find the minimum number of operations required to convert

72. Edit Distance

fin BE plan 表示 () TP delet following ive 問題描述: Given two words word1 and word2, find the minimum number of operations required to convert