Posted labuladong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
瀛︾畻娉曡鍑?nbsp;labuladong
鍚庡彴鍥炲杩涚兢涓€璧峰姏鎵p煒?/span>
璇诲畬鏈枃锛屽彲浠ュ幓鍔涙墸瑙e喅濡備笅棰樼洰锛?/span>
1143.鏈€闀?/span>鍏叡瀛愬簭鍒?/span>锛?/span>Medium锛?/span>
583. 涓や釜瀛楃涓?/span>鐨勫垹闄ゆ搷浣?/span>锛?/span>Medium锛?/span>
712.涓や釜瀛楃涓茬殑鏈€灏廇SCII鍒犻櫎鍜?/span>锛?/span>Medium锛?/span>
濂戒箙娌″啓鍔ㄦ€佽鍒掔畻娉曠浉鍏崇殑鏂囩珷浜嗭紝浠婂ぉ鏉ユ悶涓€鎶娿€?/p>
涓嶇煡閬撳ぇ瀹跺仛绠楁硶棰樻湁浠€涔堟劅瑙夛紝鎴戞€荤粨鍑烘潵鍋氱畻娉曢鐨勬妧宸у氨鏄紝鎶婂ぇ鐨勯棶棰樼粏鍖栧埌涓€涓偣锛屽厛鐮旂┒鍦ㄨ繖涓皬鐨勭偣涓婂浣曡В鍐抽棶棰橈紝鐒跺悗鍐嶉€氳繃閫掑綊/杩唬鐨勬柟寮忔墿灞曞埌鏁翠釜闂銆?/p>
姣斿璇存垜浠墠鏂?鎵嬫妸鎵嬪甫浣犲埛浜屽弶鏍戠涓夋湡锛岃В鍐充簩鍙夋爲鐨勯鐩紝鎴戜滑灏变細鎶婃暣涓棶棰樼粏鍖栧埌鏌愪竴涓妭鐐逛笂锛屾兂璞¤嚜宸辩珯鍦ㄦ煇涓妭鐐逛笂锛岄渶瑕佸仛浠€涔堬紝鐒跺悗濂椾簩鍙夋爲閫掑綊妗嗘灦灏辫浜嗐€?/p>
鍔ㄦ€佽鍒掔郴鍒楅棶棰樹篃鏄竴鏍凤紝灏ゅ叾鏄瓙搴忓垪鐩稿叧鐨勯棶棰樸€?span class="mq-62">鏈枃浠庛€屾渶闀垮叕鍏卞瓙搴忓垪闂銆嶅睍寮€锛屾€荤粨涓夐亾瀛愬簭鍒楅棶棰?/strong>锛岃В杩欓亾棰樹粩缁嗚璁茶繖绉嶅瓙搴忓垪闂鐨勫璺紝浣犲氨鑳芥劅鍙楀埌杩欑鎬濈淮鏂瑰紡浜嗐€?/p>
璁$畻鏈€闀垮叕鍏卞瓙搴忓垪锛圠ongest Common Subsequence锛岀畝绉?LCS锛夋槸涓€閬撶粡鍏哥殑鍔ㄦ€佽鍒掗鐩紝澶у搴旇閮借杩囷細 缁欎綘杈撳叆涓や釜瀛楃涓?code class="mq-67">s1鍜?code class="mq-68">s2锛岃浣犳壘鍑轰粬浠咯鐨勬渶闀垮叕鍏卞瓙搴忓垪锛岃繑鍥炶繖涓瓙搴忓垪鐨勯暱搴︺€?/p>
鍔涙墸绗?1143 棰樺氨鏄繖閬撻锛屽嚱鏁扮鍚嶅涓嬶細 姣斿璇磋緭鍏?code class="mq-77">s1 = "zabcde", s2 = "acez"锛屽畠淇╃殑鏈€闀垮叕鍏卞瓙搴忓垪鏄?code class="mq-78">lcs = "ace"锛岄暱搴︿负 3锛屾墍浠ョ畻娉曡繑鍥?3銆?/p>
濡傛灉娌℃湁鍋氳繃杩欓亾棰橈紝涓€涓渶绠€鍗曠殑鏆村姏绠楁硶灏辨槸锛屾妸 鏄剧劧锛岃繖绉嶆€濊矾鐨勫鏉傚害闈炲父楂橈紝浣犺绌蜂妇鍑烘墍鏈夊瓙搴忓垪锛岃繖涓鏉傚害灏辨槸鎸囨暟绾х殑锛岃偗瀹氫笉瀹為檯銆?/p>
姝g‘鐨勬€濊矾鏄笉瑕佽€冭檻鏁翠釜瀛楃涓诧紝鑰屾槸缁嗗寲鍒?code class="mq-84">s1鍜?code class="mq-85">s2鐨勬瘡涓瓧绗︺€傚墠鏂?瀛愬簭鍒楄В棰樻ā鏉?涓€荤粨鐨勪竴涓寰嬶細 瀵逛簬涓や釜瀛楃涓叉眰瀛愬簭鍒楃殑闂锛岄兘鏄敤涓や釜鎸囬拡 鏈€闀垮叕鍏卞瓙搴忓垪鐨勯棶棰樹篃鍙互閬靛惊杩欎釜瑙勫緥锛屾垜浠彲浠ュ厛鍐欎竴涓?code class="mq-91">dp鍑芥暟锛?/p>
杩欎釜 鏍规嵁杩欎釜瀹氫箟锛岄偅涔堟垜浠兂瑕佺殑绛旀灏辨槸 鎺ヤ笅鏉ワ紝鍜变笉瑕佺湅 鍒氭墠璇寸殑 鍏跺疄鎴戜滑涔熶笉鐭ラ亾锛岄偅灏辨妸杩欎笁绉嶆儏鍐电殑绛旀閮界畻鍑烘潵锛屽彇鍏朵腑缁撴灉鏈€澶х殑閭d釜鍛楋紝鍥犱负棰樼洰璁╂垜浠畻銆屾渶闀裤€嶅叕鍏卞瓙搴忓垪鐨勯暱搴﹀槢銆?/p>
杩欎笁绉嶆儏鍐电殑绛旀鎬庝箞绠楋紵鍥炴兂涓€涓嬫垜浠殑 浠g爜鍙互鍐嶈繘涓€姝ワ細 杩欓噷灏卞凡缁忛潪甯告帴杩戞垜浠殑鏈€缁堢瓟妗堜簡锛?span class="mq-211">杩樻湁涓€涓皬鐨勪紭鍖栵紝鎯呭喌涓夈€?code class="mq-212">s1[i]鍜?code class="mq-213">s2[j]閮戒笉鍦?lcs 涓€嶅叾瀹炲彲浠ョ洿鎺ュ拷鐣?/strong>銆?/p>
鍥犱负鎴戜滑鍦ㄦ眰鏈€澶у€煎槢锛屾儏鍐典笁鍦ㄨ绠?code class="mq-215">s1[i+1..]鍜?code class="mq-216">s2[j+1..]鐨?code class="mq-217">lcs闀垮害锛岃繖涓暱搴﹁偗瀹氭槸灏忎簬绛変簬鎯呭喌浜?code class="mq-218">s1[i..]鍜?code class="mq-219">s2[j+1..]涓殑 鍚岀悊锛屾儏鍐典笁鐨勭粨鏋滆偗瀹氫篃灏忎簬绛変簬鎯呭喌涓€銆?span class="mq-225">璇寸櫧浜嗭紝鎯呭喌涓夎鎯呭喌涓€鍜屾儏鍐典簩鍖呭惈浜?/strong>锛屾墍浠ユ垜浠彲浠ョ洿鎺ュ拷鐣ユ帀鎯呭喌涓夛紝瀹屾暣浠g爜濡備笅锛?/p>
浠ヤ笂鎬濊矾瀹屽叏灏辨槸鎸夌収鎴戜滑涔嬪墠鐨勭垎鏂?鍔ㄦ€佽鍒掑璺鏋?鏉ョ殑锛屽簲璇ユ槸寰堝鏄撶悊瑙g殑銆傝嚦浜庝负浠€涔堣鍔?code class="mq-273">memo澶囧繕褰曪紝鎴戜滑涔嬪墠鍐欒繃寰堝娆★紝涓轰簡鐓ч【鏂版潵鐨勮鑰咃紝杩欓噷鍐嶇畝鍗曢噸澶嶄竴涓嬶紝棣栧厛鎶借薄鍑烘垜浠牳蹇?code class="mq-274">dp鍑芥暟鐨勯€掑綊妗嗘灦锛?/p>
浣犵湅锛屽亣璁炬垜鎯充粠 杩欏氨鏄噸鍙犲瓙闂锛屽鏋滄垜浠笉鐢?code class="mq-297">memo澶囧繕褰曟秷闄ゅ瓙闂锛岄偅涔?code class="mq-298">dp(i+1, j+1)灏变細琚娆¤绠楋紝杩欐槸娌℃湁蹇呰鐨勩€?/p>
鑷虫锛屾渶闀垮叕鍏卞瓙搴忓垪闂灏卞畬鍏ㄨВ鍐充簡锛岀敤鐨勬槸鑷《鍚戜笅甯﹀蹇樺綍鐨勫姩鎬佽鍒掓€濊矾锛屾垜浠綋鐒朵篃鍙互浣跨敤鑷簳鍚戜笂鐨勮凯浠g殑鍔ㄦ€佽鍒掓€濊矾锛屽拰鎴戜滑鐨勯€掑綊鎬濊矾涓€鏍凤紝鍏抽敭鏄浣曞畾涔?code class="mq-300">dp鏁扮粍锛屾垜杩欓噷涔熷啓涓€涓嬭嚜搴曞悜涓婄殑瑙f硶鍚э細 鑷簳鍚戜笂鐨勮В娉曚腑 鍙﹀锛岃嚜搴曞悜涓婄殑瑙f硶鍙互閫氳繃鎴戜滑鍓嶆枃璁茶繃鐨?鍔ㄦ€佽鍒掔姸鎬佸帇缂╂妧宸?鏉ヨ繘琛屼紭鍖栵紝鎶婄┖闂村鏉傚害鍘嬬缉涓?O(N)锛岃繖閲岀敱浜庣瘒骞呮墍闄愶紝灏变笉灞曞紑浜嗐€?/p>
涓嬮潰锛屾潵鐪嬩袱閬撳拰鏈€闀垮叕鍏卞瓙搴忓垪鐩镐技鐨勪袱閬撻鐩€?/p>
杩欐槸鍔涙墸绗?583 棰樸€屼袱涓瓧绗︿覆鐨勫垹闄ゆ搷浣溿€嶏紝鐪嬩笅棰樼洰锛?/p>
棰樼洰璁╂垜浠绠楀皢涓や釜瀛楃涓插彉寰楃浉鍚岀殑鏈€灏戝垹闄ゆ鏁帮紝閭f垜浠彲浠ユ€濊€冧竴涓嬶紝鏈€鍚庤繖涓や釜瀛楃涓蹭細琚垹鎴愪粈涔堟牱瀛愶紵 鍒犻櫎鐨勭粨鏋滀笉灏辨槸瀹冧咯鐨勬渶闀垮叕鍏卞瓙搴忓垪鍢涳紒 閭d箞锛岃璁$畻鍒犻櫎鐨勬鏁帮紝灏卞彲浠ラ€氳繃鏈€闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︽帹瀵煎嚭鏉ワ細 杩欓亾棰樺氨瑙e喅浜嗭紒 杩欐槸鍔涙墸绗?712 棰橈紝鐪嬩笅棰樼洰锛?/p>
杩欓亾棰橈紝鍜屼笂涓€閬撻闈炲父绫讳技锛岃繖鍥炰笉闂垜浠垹闄ょ殑瀛楃涓暟浜嗭紝闂垜浠垹闄ょ殑瀛楃鐨?ASCII 鐮佸姞璧锋潵鏄灏戙€?/p>
閭e氨涓嶈兘鐩存帴澶嶇敤璁$畻鏈€闀垮叕鍏卞瓙搴忓垪鐨勫嚱鏁颁簡锛屼絾鏄彲浠ヤ緷鐓т箣鍓嶇殑鎬濊矾锛?span class="mq-371">绋嶅井淇敼 base case 鍜岀姸鎬佽浆绉婚儴鍒嗗嵆鍙洿鎺ュ啓鍑鸿В娉曚唬鐮?/strong>锛?/p>
base case 鏈変竴瀹氬尯鍒紝璁$畻 鍏充簬鐘舵€佽浆绉伙紝褰?code class="mq-426">s1[i]鍜?code class="mq-427">s2[j]鐩稿悓鏃朵笉闇€瑕佸垹闄わ紝涓嶅悓鏃堕渶瑕佸垹闄わ紝鎵€浠ュ彲浠ュ埄鐢?code class="mq-428">dp鍑芥暟璁$畻涓ょ鎯呭喌锛屽緱鍑烘渶浼樼殑缁撴灉銆傚叾浠栫殑澶у悓灏忓紓锛屽氨涓嶅叿浣撳睍寮€浜嗐€?/p>
鑷虫锛屼笁閬撳瓙搴忓垪闂灏辫В鍐冲畬浜嗭紝鍏抽敭鍦ㄤ簬灏嗛棶棰樼粏鍖栧埌瀛楃锛屾牴鎹瘡涓や釜瀛楃鏄惁鐩稿悓鏉ュ垽鏂粬浠槸鍚﹀湪缁撴灉瀛愬簭鍒椾腑锛屼粠鑰岄伩鍏嶄簡瀵规墍鏈夊瓙搴忓垪杩涜绌蜂妇銆?/p>
杩欎篃绠楁槸鍦ㄤ袱涓瓧绗︿覆涓眰瀛愬簭鍒楃殑甯哥敤鎬濊矾鍚э紝寤鸿濂藉ソ浣撲細锛屽澶氳仈绯粇鏈€闀垮叕鍏卞瓙搴忓垪
int longestCommonSubsequence(String s1, String s2);
s1
鍜?code class="mq-81">s2鐨勬墍鏈夊瓙搴忓垪閮界┓涓惧嚭鏉ワ紝鐒跺悗鐪嬬湅鏈夋病鏈夊叕鍏辩殑锛岀劧鍚庡湪鎵€鏈夊叕鍏卞瓙搴忓垪閲岄潰鍐嶅鎵句竴涓暱搴︽渶澶х殑銆?/p>
i
鍜?code class="mq-89">j鍒嗗埆鍦ㄤ袱涓瓧绗︿覆涓婄Щ鍔紝澶ф鐜囨槸鍔ㄦ€佽鍒掓€濊矾銆?/p>
// 瀹氫箟锛氳绠?s1[i..] 鍜?s2[j..] 鐨勬渶闀垮叕鍏卞瓙搴忓垪闀垮害
int dp(String s1, int i, String s2, int j)dp
鍑芥暟鐨勫畾涔夋槸锛?span class="mq-103">dp(s1, i, s2, j)
璁$畻s1[i..]
鍜?code class="mq-106">s2[j..]鐨勬渶闀垮叕鍏卞瓙搴忓垪闀垮害銆?/p>
dp(s1, 0, s2, 0)
锛屼笖 base case 灏辨槸i == len(s1)
鎴?code class="mq-110">j == len(s2)鏃讹紝鍥犱负杩欐椂鍊?code class="mq-111">s1[i..]鎴?code class="mq-112">s2[j..]灏辩浉褰撲簬绌轰覆浜嗭紝鏈€闀垮叕鍏卞瓙搴忓垪鐨勯暱搴︽樉鐒舵槸 0锛?/p>
int longestCommonSubsequence(String s1, String s2) {
return dp(s1, 0, s2, 0);
}
/* 涓诲嚱鏁?nbsp;*/
int dp(String s1, int i, String s2, int j) {
// base case
if (i == s1.length() || j == s2.length()) {
return 0;
}
// ...s1
鍜?code class="mq-137">s2涓や釜瀛楃涓诧紝鑰屾槸瑕佸叿浣撳埌姣忎竴涓瓧绗︼紝鎬濊€冩瘡涓瓧绗﹁鍋氫粈涔?/strong>銆?/p>
// 瀹氫箟锛氳绠?s1[i..] 鍜?s2[j..] 鐨勬渶闀垮叕鍏卞瓙搴忓垪闀垮害
int dp(String s1, int i, String s2, int j) {
if (s1.charAt(i) == s2.charAt(j)) {
// s1[i] 鍜?nbsp;s2[j] 蹇呯劧鍦?nbsp;lcs 涓紝
// 鍔犱笂 s1[i+1..] 鍜?nbsp;s2[j+1..] 涓殑 lcs 闀垮害锛屽氨鏄瓟妗?/span>
return 1 + dp(s1, i + 1, s2, j + 1)
} else {
// ...
}
}s1[i] == s2[j]
鐨勬儏鍐碉紝浣嗗鏋?code class="mq-171">s1[i] != s2[j]锛屽簲璇ユ€庝箞鍔炲憿锛?/p>
s1[i] != s2[j]
鎰忓懗鐫€锛?code class="mq-175">s1[i]鍜?code class="mq-176">s2[j]涓嚦灏戞湁涓€涓瓧绗︿笉鍦?code class="mq-177">lcs涓?/strong>锛?/p>
dp
鍑芥暟瀹氫箟锛屼笉灏辨槸涓撻棬涓轰簡璁$畻瀹冧滑鑰岃璁$殑鍢涳紒// 瀹氫箟锛氳绠?s1[i..] 鍜?s2[j..] 鐨勬渶闀垮叕鍏卞瓙搴忓垪闀垮害
int dp(String s1, int i, String s2, int j) {
if (s1.charAt(i) == s2.charAt(j)) {
return 1 + dp(s1, i + 1, s2, j + 1)
} else {
// s1[i] 鍜?nbsp;s2[j] 涓嚦灏戞湁涓€涓瓧绗︿笉鍦?nbsp;lcs 涓紝
// 绌蜂妇涓夌鎯呭喌鐨勭粨鏋滐紝鍙栧叾涓殑鏈€澶х粨鏋?/span>
return max(
// 鎯呭喌涓€銆乻1[i] 涓嶅湪 lcs 涓?/span>
dp(s1, i + 1, s2, j),
// 鎯呭喌浜屻€乻2[j] 涓嶅湪 lcs 涓?/span>
dp(s1, i, s2, j + 1),
// 鎯呭喌涓夈€侀兘涓嶅湪 lcs 涓?/span>
dp(s1, i + 1, s2, j + 1)
);
}
}lcs
闀垮害鐨勶紝鍥犱负s1[i+1..]
姣?code class="mq-222">s1[i..]鐭槢锛岄偅浠庤繖閲岄潰绠楀嚭鐨?code class="mq-223">lcs褰撶劧涔熶笉鍙兘鏇撮暱鍢涖€?/p>
// 澶囧繕褰曪紝娑堥櫎閲嶅彔瀛愰棶棰?/span>
int[][] memo;
/* 涓诲嚱鏁?nbsp;*/
int longestCommonSubsequence(String s1, String s2) {
int m = s1.length(), n = s2.length();
// 澶囧繕褰曞€间负 -1 浠h〃鏈浘璁$畻
memo = new int[m][n];
for (int[] row : memo)
Arrays.fill(row, -1);
// 璁$畻 s1[0..] 鍜?nbsp;s2[0..] 鐨?nbsp;lcs 闀垮害
return dp(s1, 0, s2, 0);
}
// 瀹氫箟锛氳绠?s1[i..] 鍜?s2[j..] 鐨勬渶闀垮叕鍏卞瓙搴忓垪闀垮害
int dp(String s1, int i, String s2, int j) {
// base case
if (i == s1.length() || j == s2.length()) {
return 0;
}
// 濡傛灉涔嬪墠璁$畻杩囷紝鍒欑洿鎺ヨ繑鍥炲蹇樺綍涓殑绛旀
if (memo[i][j] != -1) {
return memo[i][j];
}
// 鏍规嵁 s1[i] 鍜?nbsp;s2[j] 鐨勬儏鍐靛仛閫夋嫨
if (s1.charAt(i) == s2.charAt(j)) {
// s1[i] 鍜?nbsp;s2[j] 蹇呯劧鍦?nbsp;lcs 涓?/span>
memo[i][j] = 1 + dp(s1, i + 1, s2, j + 1);
} else {
// s1[i] 鍜?nbsp;s2[j] 鑷冲皯鏈変竴涓笉鍦?nbsp;lcs 涓?/span>
memo[i][j] = Math.max(
dp(s1, i + 1, s2, j),
dp(s1, i, s2, j + 1)
);
}
return memo[i][j];
}int dp(int i, int j) {
dp(i + 1, j + 1); // #1
dp(i, j + 1); // #2
dp(i + 1, j); // #3
}dp(i, j)
杞Щ鍒?code class="mq-292">dp(i+1, j+1)锛屾湁涓嶆涓€绉嶆柟寮忥紝鍙互鐩存帴璧?code class="mq-293">#1锛屼篃鍙互璧?code class="mq-294">#2 -> #3锛屼篃鍙互璧?code class="mq-295">#3 -> #2銆?/p>
int longestCommonSubsequence(String s1, String s2) {
int m = s1.length(), n = s2.length();
int[][] dp = new int[m + 1][n + 1];
// 瀹氫箟锛歴1[0..i-1] 鍜?s2[0..j-1] 鐨?lcs 闀垮害涓?dp[i][j]
// 鐩爣锛歴1[0..m-1] 鍜?s2[0..n-1] 鐨?lcs 闀垮害锛屽嵆 dp[m][n]
// base case: dp[0][..] = dp[..][0] = 0
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
// 鐜板湪 i 鍜?nbsp;j 浠?nbsp;1 寮€濮嬶紝鎵€浠ヨ鍑忎竴
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
// s1[i-1] 鍜?nbsp;s2[j-1] 蹇呯劧鍦?nbsp;lcs 涓?/span>
dp[i][j] = 1 + dp[i - 1][j - 1];
} else {
// s1[i-1] 鍜?nbsp;s2[j-1] 鑷冲皯鏈変竴涓笉鍦?nbsp;lcs 涓?/span>
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
return dp[m][n];
}dp
鏁扮粍瀹氫箟鐨勬柟寮忓拰鎴戜滑鐨勯€掑綊瑙f硶鏈変竴鐐瑰樊寮傦紝鑰屼笖鐢变簬鏁扮粍绱㈠紩浠?0 寮€濮嬶紝鏈夌储寮曞亸绉伙紝涓嶈繃鎬濊矾鍜屾垜浠殑閫掑綊瑙f硶瀹屽叏鐩稿悓锛屽鏋滀綘鐪嬫噦浜嗛€掑綊瑙f硶锛岃繖涓В娉曞簲璇ヤ笉闅剧悊瑙c€?/p>
瀛楃涓茬殑鍒犻櫎鎿嶄綔
int minDistance(String s1, String s2);
int minDistance(String s1, String s2) {
int m = s1.length(), n = s2.length();
// 澶嶇敤鍓嶆枃璁$畻 lcs 闀垮害鐨勫嚱鏁?/span>
int lcs = longestCommonSubsequence(s1, s2);
return m - lcs + n - lcs;
}鏈€灏?ASCII 鍒犻櫎鍜?/span>
// 澶囧繕褰?/span>
int memo[][];
/* 涓诲嚱鏁?nbsp;*/
int minimumDeleteSum(String s1, String s2) {
int m = s1.length(), n = s2.length();
// 澶囧繕褰曞€间负 -1 浠h〃鏈浘璁$畻
memo = new int[m][n];
for (int[] row : memo)
Arrays.fill(row, -1);
return dp(s1, 0, s2, 0);
}
// 瀹氫箟锛氬皢 s1[i..] 鍜?s2[j..] 鍒犻櫎鎴愮浉鍚屽瓧绗︿覆锛?/span>
// 鏈€灏忕殑 ASCII 鐮佷箣鍜屼负 dp(s1, i, s2, j)銆?/span>
int dp(String s1, int i, String s2, int j) {
int res = 0;
// base case
if (i == s1.length()) {
// 濡傛灉 s1 鍒板ご浜嗭紝閭d箞 s2 鍓╀笅鐨勯兘寰楀垹闄?/span>
for (; j < s2.length(); j++)
res += s2.charAt(j);
return res;
}
if (j == s2.length()) {
// 濡傛灉 s2 鍒板ご浜嗭紝閭d箞 s1 鍓╀笅鐨勯兘寰楀垹闄?/span>
for (; i < s1.length(); i++)
res += s1.charAt(i);
return res;
}
if (memo[i][j] != -1) {
return memo[i][j];
}
if (s1.charAt(i) == s2.charAt(j)) {
// s1[i] 鍜?nbsp;s2[j] 閮芥槸鍦?nbsp;lcs 涓殑锛屼笉鐢ㄥ垹闄?/span>
memo[i][j] = dp(s1, i + 1, s2, j + 1);
} else {
// s1[i] 鍜?nbsp;s2[j] 鑷冲皯鏈変竴涓笉鍦?nbsp;lcs 涓紝鍒犱竴涓?/span>
memo[i][j] = Math.min(
s1.charAt(i) + dp(s1, i + 1, s2, j),
s2.charAt(j) + dp(s1, i, s2, j + 1)
);
}
return memo[i][j];
}lcs
闀垮害鏃讹紝濡傛灉涓€涓瓧绗︿覆涓虹┖锛岄偅涔?code class="mq-424">lcs闀垮害蹇呯劧鏄?0锛涗絾鏄繖閬撻濡傛灉涓€涓瓧绗︿覆涓虹┖锛屽彟涓€涓瓧绗︿覆蹇呯劧瑕佽鍏ㄩ儴鍒犻櫎锛屾墍浠ラ渶瑕佽绠楀彟涓€涓瓧绗︿覆鎵€鏈夊瓧绗︾殑 ASCII 鐮佷箣鍜屻€?/p>
寰€鏈熸帹鑽?nbsp;馃敆
锛匡伎锛匡伎锛匡伎锛匡伎锛匡伎锛匡伎锛?/span>
瀛﹀ソ绠楁硶闈犲璺紝璁ゅ噯 labuladong锛?/strong>鐭ヤ箮銆丅绔欒处鍙峰悓鍚嶃€?/strong>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章