Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit44474a1

Browse files
authored
add cleaner solution
1 parenta25fff3 commit44474a1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎Kangli/DP/editDistance.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,28 @@ def minDistance(self, word1, word2):
1313
dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(word1[i-1]!=word2[j-1]))
1414
returndp[-1][-1]
1515

16+
#sample 282 ms submission
17+
classSolution(object):
18+
defminDistance(self,word1,word2):
19+
"""
20+
:type word1: str
21+
:type word2: str
22+
:rtype: int
23+
"""
24+
m=len(word1)
25+
n=len(word2)
26+
dp= [[0forxinxrange(n+1)]forxinxrange(m+1)]
27+
28+
foriinxrange(m+1):
29+
forjinxrange(n+1):
30+
ifi==0:
31+
dp[i][j]=j
32+
elifj==0:
33+
dp[i][j]=i
34+
elifword1[i-1]==word2[j-1]:
35+
dp[i][j]=dp[i-1][j-1]
36+
else:
37+
dp[i][j]=1+min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])
38+
39+
returndp[m][n]
1640

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp