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

Commit e4ede15

Browse files
Update 0072.编辑距离 C语言版本
1 parent f438788 commit e4ede15

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0072.编辑距离.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,32 @@ function minDistance(word1: string, word2: string): number {
362362
};
363363
```
364364

365+
C:
365366

367+
```c
368+
int min(int num1, int num2, int num3) {
369+
return num1 > num2 ? (num2 > num3 ? num3 : num2) : (num1 > num3 ? num3 : num1);
370+
}
371+
372+
int minDistance(char * word1, char * word2){
373+
int dp[strlen(word1)+1][strlen(word2)+1];
374+
dp[0][0] = 0;
375+
for (int i = 1; i <= strlen(word1); i++) dp[i][0] = i;
376+
for (int i = 1; i <= strlen(word2); i++) dp[0][i] = i;
377+
378+
for (int i = 1; i <= strlen(word1); i++) {
379+
for (int j = 1; j <= strlen(word2); j++) {
380+
if (word1[i-1] == word2[j-1]) {
381+
dp[i][j] = dp[i-1][j-1];
382+
}
383+
else {
384+
dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1;
385+
}
386+
}
387+
}
388+
return dp[strlen(word1)][strlen(word2)];
389+
}
390+
```
366391
367392
-----------------------
368393
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /