JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 public class Solution {
 2     public int minDistance(String word1, String word2) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         int m = word1.length(), n = word2.length();
 6         int[][] ed = new int[m + 1][n + 1];
 7         for(int i = 0; i < m + 1; i++){
 8             for(int j = 0; j < n + 1; j++){
 9                 if(i == 0){
10                     ed[i][j] = j;
11                 } else if(j == 0){
12                     ed[i][j] = i;
13                 } else {
14                     if(word1.charAt(i - 1) == word2.charAt(j - 1)){
15                         ed[i][j] = ed[i - 1][j - 1];
16                     } else {
17                         ed[i][j] = 1 + Math.min(ed[i - 1][j - 1], Math.min(ed[i][j - 1], ed[i - 1][j]));
18                     }
19                 }
20             }
21         }
22         return ed[m][n];
23     }
24 }

 

posted on 2013-11-18 18:15  JasonChang  阅读(168)  评论(0编辑  收藏  举报