72. Edit Distance 编辑距离
2017-09-13 13:29
495 查看
题目:
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
/*
解题思路:采用动态规划,Word1和Word2从空串开始,所以dp[i][j]的长度为word1.length()+1、word2.length()+2
首先初始化第一行和第一列,由于插入。修改、删除的代价都是1,所以第一行第一列dp[0][i]=i,dp[i][0]=i
递推过程,如果s1[i]==s2[j],则不用修改,其dp[i][j]的值为的dp[i-1][j-1];
如果s1[i]!=s2[j],则判断dp[i-1][j]+1,表示插入字符
dp[i][j-1]+1,表示删除字符
dp[i-1][j-1]+1表示修改字符
谁比较小,选取较小的值作为dp[i][j]的值
*/
public int minDistance(String word1, String word2) {
if(word1.length()==0){
return word2.length();
}
if(word2.length()==0){
return word1.length();
}
char[] s1=word1.toCharArray();
char[] s2=word2.toCharArray();
int[][] dp=new int[s1.length+1][s2.length+1];
//初始化第一行
for(int i=0;i<=s2.length;i++){
dp[0][i]=i;
}
//初始化第一列
for(int i=0;i<=s1.length;i++){
dp[i][0]=i;
}
for(int i=1;i<=s1.length;i++){
for(int j=1;j<=s2.length;j++){
if(s1[i-1]==s2[j-1]){
dp[i][j]=dp[i-1][j-1];
}else{
int temp=Math.min(dp[i-1][j]+1,dp[i][j-1]+1);
dp[i][j]=Math.min(temp,dp[i-1][j-1]+1);
}
}//for
}//for
return dp[s1.length][s2.length];
}
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
/*
解题思路:采用动态规划,Word1和Word2从空串开始,所以dp[i][j]的长度为word1.length()+1、word2.length()+2
首先初始化第一行和第一列,由于插入。修改、删除的代价都是1,所以第一行第一列dp[0][i]=i,dp[i][0]=i
递推过程,如果s1[i]==s2[j],则不用修改,其dp[i][j]的值为的dp[i-1][j-1];
如果s1[i]!=s2[j],则判断dp[i-1][j]+1,表示插入字符
dp[i][j-1]+1,表示删除字符
dp[i-1][j-1]+1表示修改字符
谁比较小,选取较小的值作为dp[i][j]的值
*/
public int minDistance(String word1, String word2) {
if(word1.length()==0){
return word2.length();
}
if(word2.length()==0){
return word1.length();
}
char[] s1=word1.toCharArray();
char[] s2=word2.toCharArray();
int[][] dp=new int[s1.length+1][s2.length+1];
//初始化第一行
for(int i=0;i<=s2.length;i++){
dp[0][i]=i;
}
//初始化第一列
for(int i=0;i<=s1.length;i++){
dp[i][0]=i;
}
for(int i=1;i<=s1.length;i++){
for(int j=1;j<=s2.length;j++){
if(s1[i-1]==s2[j-1]){
dp[i][j]=dp[i-1][j-1];
}else{
int temp=Math.min(dp[i-1][j]+1,dp[i][j-1]+1);
dp[i][j]=Math.min(temp,dp[i-1][j-1]+1);
}
}//for
}//for
return dp[s1.length][s2.length];
}
相关文章推荐
- LeetCode刷题 | 72. Edit Distance 难题 编辑距离dp
- LeetCode-72. Edit Distance (JAVA)字符串最小编辑距离DP&DFS
- LeetCode 72. Edit Distance(编辑距离)
- 72. Edit Distance 两个单词的编辑距离
- [LeetCode] 72. Edit Distance 编辑距离
- Leetcode 72. Edit Distance 编辑距离 解题报告
- LeetCode 72. Edit Distance(编辑距离)
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
- leetcode:Edit Distance 编辑距离
- hdu 1516 String Distance and Transform Process(编辑距离+保存修改路径)
- 斯坦福大学自然语言处理第三课“最小编辑距离(Minimum Edit Distance)”
- 最小编辑距离(Minimum edit distance)
- hdu 1516 String Distance and Transform Process(编辑距离+记录路径)
- stanford NLP第三课“最小编辑距离(Minimum Edit Distance)”
- 编辑距离(Minimum Edit Distance)
- SPOJ 6219 Edit distance字符串间编辑距离
- 最小编辑距离 | Minimum Edit Distance
- “最小编辑距离(Minimum Edit Distance)”
- 字符串编辑距离(Edit Distance)分析和源代码
- hdu-1516 String Distance and Transform Process(编辑距离+路径)