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】编辑距离的更多相关文章

  1. leetcode 编辑距离

    class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...

  2. LeetCode 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...

  3. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  4. [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 ...

  5. [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)

    https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...

  6. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

  7. [LeetCode]72. 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...

  8. [leetcode] 72. 编辑距离(二维动态规划)

    72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...

  9. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  10. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. Headfirst设计模式的C++实现——命令模式(Command)

    先看如果不用命令模式的实现: light.h #ifndef _LIGHT_H_ #define _LIGHT_H #include <iostream> class LIGHT { pu ...

  2. mini2440移植uboot-2008.10 遇到的问题

    1.mkimage的使用(u-boot-2008.10/tools/mkimage) 首先./mkimage 运行或者 将mkimage 拷贝到 /bin 目录下面 法一: #mkimage -n ' ...

  3. 浏览器测试功能(jquery1.9以后已取消)

    // 1.9以后取消了msie这些私有方法判断.这里封装加回. var matched = (function(ua) { ua = ua.toLowerCase(); var match = /(o ...

  4. HTML XML XHTML DHTML区别与联系

    (1)HTML HTML是超文本标记语言 (2)XML XML是可扩展标识语言,但XML是一种能定义其他语言的语. XML最初设计的目的是弥补HTML的不足, 以强大的扩展性满足网络信息发布的需要 , ...

  5. php返回json数据函数实例

    本文实例讲述了php返回json数据函数的用法,分享给大家供大家参考.具体方法如下: json_encode()函数用法: ? 1 echo json_encode(array('a'=>'bb ...

  6. ecshop在nginx下实现负载均衡

    ecshop在负载方面的功能是十分弱小的.当你的IP每个小时到达了一万IP.如果在带宽和服务器硬件有限的情况下.你的服务器很快就会崩溃的.网站直接挂掉.为了增强ecshop在负载均衡方面的能力.我们可 ...

  7. Python的面向对象4

    今天我们接着来聊聊继承! 那什么是继承呢? 新写的类是不必重新编写,只要从现有的类继承,就自动拥有了该类的所有功能,新类只需要编写现有类缺少的功能,可以复用已有的代码! python的继承的特点: 继 ...

  8. Ubuntu15.10 编译VLC Android(安卓)过程记录

    持续更新中... 最后一次修改于 2016-03-20 15:33:45 1.必要库的安装 除基本编译环境(gcc.g++等外),需要额外安装如下的库(用于下载必要的依赖文件) (1)JDK 推荐安装 ...

  9. Entity Framework Code First 数据迁移

    需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...

  10. handoff了解

    iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行.这个在iOS8 ...