【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 ...
随机推荐
- C++学习指南
转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...
- 状态模式(State Pattern)
状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类. 这个模式将状态封装成为独立的类,并将动作委托到代表当前对象的对象,这样行为就与拥有状态类解耦了. 从客户的角度来看,对象的 ...
- php 商务网站购物车联动地址
数据表如下: CREATE TABLE IF NOT EXISTS `china` (`region_id` smallint(5) unsigned NOT NULL, `parent_id` s ...
- ubuntu基本操作(2)
查看当前使用那种 shell echo $SHELL 更换 shell 类型:首先检查是否安装了相应的类型 shell开启终端,直接输入相应的 shell 名称如果没有安装,则先安装,否则直接启动此时 ...
- 数据库创建&数据表创建
--第2_1题创建数据库 create database Student201038897041 on primary (name='student1', filename='F:\coures\SQ ...
- http 常用状态码及含义
http://www.kuaipan.cn/developers/document_status.htm
- 操作系统和Python的发展历程
一:操作系统的发展历史: 操作系统:什么是操作系统?我们首先想到的是电脑,,也就是所谓的Windows8,Windows7,或者XP系统和Windows10,当然也包括我们手机的安卓系统或者IPhon ...
- Ubuntu 12.04如何从登录界面登录root
root登录,可以使我们拥有管理系统最高的权限,但是随之带来的也是,系统的安全得不到足够的保障.Ubuntu官方资料说不推荐我们以root方式登录到系统中,但是如果我们真想这么做,也是可以的. 不同版 ...
- a href="#"与a href="####"的区别是什么
前提是 :有滚动条. [感谢 黎明就在眼前 博客园园友] '#' 是有特殊意义,如果 '#' 后有内容会被认为是一个标签而从页面找到相应标签跳转到该处,找不到时会跳到页首,通常情况下使用“#ID”来 ...
- vs2010 使用SignalR 提高B2C商城用户体验(三)
vs2010 使用SignalR 提高B2C商城用户体验(三) 上一章节,我们的web即时通讯已经可以实现跨域了,但针对我们的需求,还希望,一些客户端程序可以和我们的web用户,在线聊天,所以到Sig ...