GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/EditDistance.java at master · LeBW/leetcode

Expand file tree

/

EditDistance.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
30 lines (30 loc) · 883 Bytes
/

EditDistance.java

Copy path

File metadata and controls

30 lines (30 loc) · 883 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 72. Edit Distance
* 最短编辑距离。 动态规划经典题目
* @author LBW
*/
public class EditDistance {
public int minDistance(String word1, String word2) {
int l1 = word1.length(), l2 = word2.length();
int[][] dp = new int[l1+1][l2+1];
// init
for (int i = 0; i <= l1; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= l2; j++) {
dp[0][j] = j;
}
// fill the dp array.
for (int i = 1; i <= l1; i++) {
for (int j = 1; j <= l2; j++) {
if (word1.charAt(i-1) == word2.charAt(j-1)) {
dp[i][j] = dp[i-1][j-1];
}
else {
dp[i][j] = Math.min(Math.min(dp[i][j-1] + 1, dp[i-1][j] + 1), dp[i-1][j-1] + 1);
}
}
}
return dp[l1][l2];
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.