【leetcode】编辑距离
dp方程“
1、初始化;dp[0][i]=i; dp[j][0]=j;
2.dp[i][j]= dp[i-1][j-1](相等)
dp[i-1][j]+1 ,,dp[i][j-1]+1; dp[i-1][j-1] (这个对应是改的况)
注意字符串下标开始位置就OK了
public class Solution {
public int minDistance(String word1, String word2) {
char c1[]=word1.toCharArray();
char c2[]=word2.toCharArray();
int len1=word1.length();
int len2=word2.length();
int dp[][]=new int[len1+1][len2+1];
int i,j;
for(i=0;i<len2+1;i++) dp[0][i]=i;
for(j=0;j<len1+1;j++) dp[j][0]=j;
for(i=1;i<len1+1;i++)
{
for( j=1;j<len2+1;j++)
{
if(c1[i-1]==c2[j-1]) dp[i][j]=dp[i-1][j-1];
else{
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+1;
dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+1);
}
}
}
return dp[len1][len2];
}
}
【leetcode】编辑距离的更多相关文章
- leetcode 编辑距离
class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...
- LeetCode 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
- [LeetCode]72. 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...
- [leetcode] 72. 编辑距离(二维动态规划)
72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- Leaflet交流
GIS科研网 Leaflet交流 谢绝转载 http://www.3sbase.com欢迎加群交流 108299288 http://www.3sbase.com/3sbase/webgistest ...
- C++类继承内存布局(一)
转自:http://blog.csdn.net/jiangyi711/article/details/4890889# 一 类布局 不同的继承方式将导致不同的内存布局 1)C结构 C++基于C,所以C ...
- 不对称密钥密码体系之RSA
公钥密码的特性: 1.加密和解密使用不同的钥匙 2.从一个钥匙推出另一个钥匙在计算上不可行 3.每个钥匙都可以做加密和解密 RSA算法: 1978年, MIT三位数学家 R.L.Rivest,A.Sh ...
- Plugin 'FEDERATED' is disabled 或 1067错误 启动错误与“服务 mysql 意外停止”解决方法
MYSQL启动报1067错误,系统日志中是“服务 mysql 意外停止” Mysql日志中则是:“Plugin 'FEDERATED' is disabled” 网我在网上找到解决方案:1.在MY.I ...
- ubuntu zendDebugger.so 加载不上的问题
参考文章 http://blog.sina.com.cn/s/blog_6612d5810101dapf.html 装zenDdebugger是为了在eclipse中调试用!!!!!!!结果搞了半 ...
- Java---Hibernate>>Can't create table './xxx/#sql-b2c_1a.frm' (errno: xxx)解决方法
通用方案:删除相关表,重新生成. 1.关联表之间数据引擎不一致导致: 修改相关表的引擎设定,保持一致. 2.关联表索引字段的引用类型不一样(如A表关联字段是int,B表索引是char): 修改相关表的 ...
- cakephp recursive -1,0,1,2 速查
-1 : model本身 0 :model本身 + belongTo + hasOne 1 :model本身 + belongTo + hasOne + hasMany 2: :model ...
- web api 跨域请求,ajax跨域调用webapi
1.跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服 ...
- object 属性 对象的继承 (原型, call,apply)
object 为一切对象的基类! 属性:constructor: 对创建对象的函数的引用Prototype: 原型(类型) hasOwnProperty(property):判断对象是否有某个特定的属 ...
- c#解决数据库用in的时候大于1000报错问题
问题: //oracle数据库报错 ,,,,,,,.....); c#写了一个方法解决 /// <summary> /// 拼接sql /// </summary> /// & ...