leetcode 72 edit distance
来源:互联网 发布:ie11启用javascript 编辑:程序博客网 时间:2024/05/21 03:24
求两个字符串的编辑距离。核心思想依然是动态规划,但是递归的时间复杂度太大,尽量用数组解决这种问题。
public int minDistance(String word1, String word2) {
int len1=word1.length()+1;
int len2=word2.length()+1;
int[][] s=new int[len1][len2];
for(int i=0;i<len1;i++){
s[i][0]=i;
}
for(int j=0;j<len2;j++){
s[0][j]=j;
}
for(int i=1;i<len1;i++){
for(int j=1;j<len2;j++){
if(word1.charAt(i-1)==word2.charAt(j-1)){
s[i][j]=s[i-1][j-1];
}
else{
s[i][j]=s[i-1][j-1]+1;
}
s[i][j]=min(s[i][j], s[i-1][j]+1,s[i][j-1]+1);
}
}
return s[len1-1][len2-1];
}
public int min(int a, int b, int c) {
if (a < b) {
if (b < c) {
return a;
} else {
if (a < c) {
return a;
} else {
return c;
}
}
} else {
if (a < c) {
return b;
} else {
if (b < c) {
return b;
} else {
return c;
}
}
}
}
阅读全文
0 0
- LeetCode(72)Edit Distance
- LeetCode 72 Edit Distance
- [leetcode 72] Edit Distance
- [leetcode] 72 Edit Distance
- leetcode || 72、Edit Distance
- Edit Distance Leetcode 72
- Edit Distance - LeetCode 72
- leetcode 72:Edit Distance
- [leetcode 72]Edit Distance
- leetcode 72: Edit Distance
- 【Leetcode】Edit Distance #72
- Leetcode#72||Edit Distance
- leetcode 72:Edit Distance
- LeetCode 72 Edit Distance
- leetcode 72 Edit Distance
- Leetcode (72) Edit Distance
- LeetCode 72 Edit Distance
- Leetcode Edit Distance 72
- 关于商业和创业的一点点思考
- mysql中大于等于某个年月,小于等于当前年月之间的所有年月的语句如何写呢
- R语言网页爬虫
- SQL:将多条记录的不同数据放入同一格中
- MySQL权限管理
- leetcode 72 edit distance
- CPSR_C ,CPSR_cxsf 详解
- 使用Apache POI中的XSSF导出Excel
- html-meta标签
- Redis源码阅读笔记—adlist
- Lua 基础之函数
- 05-条件语句
- Ubuntu14.04环境下opencv2.4.9的配置
- 封了1000多个IP地址段,服务器现在坚如磐石,对付几个小毛贼还是很轻松的