摘要:給出兩個(gè)解,一個(gè)是填表,一個(gè)是記憶化搜索。因?yàn)樘畋硪欢〞?huì)把的表填滿(mǎn)。走出來(lái)的則是一條從起點(diǎn)到終點(diǎn)的線,不會(huì)填滿(mǎn)整個(gè)表。時(shí)間退化到,變成找路徑的時(shí)間。
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
給出兩個(gè)解,一個(gè)是填dp表,一個(gè)是記憶化搜索。效果是memorized search更好。 因?yàn)閐p填表一定會(huì)把O(m*n)的dp表填滿(mǎn)。 memorized search走出來(lái)的則是一條從起點(diǎn)到終點(diǎn)的線,不會(huì)填滿(mǎn)整個(gè)表。時(shí)間退化到O(m+n),變成找路徑的時(shí)間。
public class Solution { public int minDistance(String word1, String word2) { int m = word1.length(); int n = word2.length(); int[][] dp = new int[m+1][n+1]; for(int i=0; i<=m; i++) { for(int j=0; j<=n;j++) { if(i == 0) { dp[i][j] = j; } else if( j == 0){ dp[i][j] = i; } else if(word1.charAt(i-1) == word2.charAt(j-1) ) { dp[i][j] = dp[i-1][j-1]; } else { dp[i][j] = 1 + Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]); } } } return dp[m][n]; } }
public class Solution { int[][] dp; public int minDistance(String word1, String word2) { dp = new int[word1.length()][word2.length()]; return minDistanceHelper(word1, word2, 0, 0); } private int minDistanceHelper(String word1, String word2, int index1, int index2) { if (index1 == word1.length()) return word2.length() - index2; if (index2 == word2.length()) return word1.length() - index1; if (dp[index1][index2] > 0) return dp[index1][index2]; int result; if (word1.charAt(index1) == word2.charAt(index2)) { result = minDistanceHelper(word1, word2, index1+1, index2+1); } else { // replace char "abac" , "abdc" replace "a" with "d" result = 1 + minDistanceHelper(word1, word2, index1+1, index2+1); // insert char into word1 "abc" , "abdc" insert "d" result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1, index2+1)); // delete char from word1 "abdc" , "abc" delete "d" result = Math.min(result, 1 + minDistanceHelper(word1, word2, index1+1, index2)); } dp[index1][index2] = result; return result; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66929.html
摘要:復(fù)雜度思路考慮用二維來(lái)表示變換的情況。如果兩個(gè)字符串中的字符相等,那么如果兩個(gè)字符串中的字符不相等,那么考慮不同的情況表示的是,從字符串到的位置轉(zhuǎn)換到字符串到的位置,所需要的最少步數(shù)。 LeetCode[72] Edit Distance Given two words word1 and word2, find the minimum number of steps require...
摘要:題目要求輸入兩個(gè)字符串和,允許對(duì)進(jìn)行插入,刪除和替換的操作,計(jì)算出將轉(zhuǎn)化為所需要的最少的操作數(shù)。其中存儲(chǔ)的是轉(zhuǎn)換為的最小步數(shù)。首先從邊緣情況開(kāi)始考慮。只要在此基礎(chǔ)上再進(jìn)行一次插入操作即可以完成轉(zhuǎn)換。 題目要求 Given two words word1 and word2, find the minimum number of steps required to convert wor...
摘要:比較長(zhǎng)度法復(fù)雜度時(shí)間空間思路雖然我們可以用的解法,看是否為,但中會(huì)超時(shí)。這里我們可以利用只有一個(gè)不同的特點(diǎn)在時(shí)間內(nèi)完成。 One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. 比較長(zhǎng)度法 復(fù)雜度 時(shí)間 O(N) 空間 O(1) 思路 雖然我們可以用...
摘要:復(fù)雜度思路考慮如果兩個(gè)字符串的長(zhǎng)度,是肯定當(dāng)兩個(gè)字符串中有不同的字符出現(xiàn)的時(shí)候,說(shuō)明之后的字符串一定要相等。的長(zhǎng)度比較大的時(shí)候,說(shuō)明的時(shí)候,才能保證距離為。 LeetCode[161] One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. Stri...
此專(zhuān)欄文章是對(duì)力扣上算法題目各種方法的總結(jié)和歸納, 整理出最重要的思路和知識(shí)重點(diǎn)并以思維導(dǎo)圖形式呈現(xiàn), 當(dāng)然也會(huì)加上我對(duì)導(dǎo)圖的詳解. 目的是為了更方便快捷的記憶和回憶算法重點(diǎn)(不用每次都重復(fù)看題解), 畢竟算法不是做了一遍就能完全記住的. 所以本文適合已經(jīng)知道解題思路和方法, 想進(jìn)一步加強(qiáng)理解和記憶的朋友, 并不適合第一次接觸此題的朋友(可以根據(jù)題號(hào)先去力扣看看官方題解, 然后再看本文內(nèi)容). 關(guān)...
閱讀 1157·2021-11-25 09:43
閱讀 1588·2021-10-25 09:47
閱讀 2481·2019-08-30 13:46
閱讀 766·2019-08-29 13:45
閱讀 1295·2019-08-26 13:29
閱讀 3002·2019-08-23 15:30
閱讀 1117·2019-08-23 14:17
閱讀 1337·2019-08-23 13:43