这样的字符转换的dp挺经典的,

若word1[i+1]==word2[j+1] dp[i+1][j+1] = dp[i][j];否则,dp[i+1][j+1] = dp[i][j] + 1。(替换原则),dp[i+1][j+1]还能够取dp[i][j+1]与dp[i+1][j]中的较小值。(删除加入原则)
class Solution {
public:
int minDistance(string word1, string word2) {
int dp[word1.size()+1][word2.size()+1];
for(int i = 0; i < word1.size()+1; i++)
dp[i][0] = i;
for(int i = 0; i < word2.size()+1; i++)
dp[0][i] = i;
for(int i = 0; i < word1.size(); i++) {
for(int j = 0; j < word2.size(); j++) {
if(word1[i] == word2[j])
dp[i+1][j+1] = dp[i][j];
else
dp[i+1][j+1] = min(min(dp[i][j],dp[i][j+1]),dp[i+1][j])+1;
}
}
return dp[word1.size()][word2.size()];
}
};

edit distance leetcode的更多相关文章

  1. Edit Distance leetcode java

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

  2. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to 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] Edit Distance 编辑距离

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

  5. LeetCode One Edit Distance

    原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...

  6. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

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

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. [Leetcode Week8]Edit Distance

    Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...

随机推荐

  1. linux 在批处理中,完整路径有空格的处理方式(加引號)

    cp -f E:/XML_EDITOR/xmleditor25/xmleditor/Editor_UIOuterCtrl/TraceViewDlg.cpp E:/XML_EDITOR/'XMLEdit ...

  2. crm操作知识库文章实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages;     using Microsoft ...

  3. 大话NoSql

    之前看过一本名叫<<大数据挑战的书>>.里面主要讲了NOSQL的内容,感觉讲得确实不错,今天来又一次温习一下,我们大话NOSQL.说道NOSQL.我们肯定联想到的内容就是Big ...

  4. java排列

    排列:它可以被看作是多个相同类型的数据的组合,这些数据的统一管理. 1.声明. 创建 一维:type[]  var  比如:int[] a或 int a[]: 数组名= new  数组元素的类型[数组 ...

  5. kaggle之旧金山犯罪

    kaggle地址 github地址 特点: 离散特征 离散特征二值化处理 数据概览 import pandas as pd import numpy as np # 载入数据 train = pd.r ...

  6. python 笔记1--基础类型

    list 操作 append() 添加最外面 insert(pos,content) 插入指定地方 pop() 删除最外面 pop(pos) 删除指定地方 list中可以有list,且能用二维数组的方 ...

  7. 【Java基础】几种简单的调用关系与方法

    直接上代码吧. class lesson4AB //同一个类下的public修饰的方法A,B可以相互调用 { public void A() { B();//等价于this.B(); } public ...

  8. tomcat https jks 沃通免费证书安装 解决方案

    网上百度了一天什么没百度到,最后谷歌到了一篇文章启发之下解决之. 代理谷歌网站推荐一个,可以直接上谷歌: https://www.yundou.info ----------------------- ...

  9. cocoapods导入第三方库

    1.移除现有Ruby默认源 终端:gem sources --remove https://rubygems.org/ 2.使用新的源 终端:gem sources -a https://ruby.t ...

  10. (原)STL中vector的疑问

    以前基本上没有用过STL,当然包括里面的vector.今天试验了一下. 主要看了这个网址: http://blog.csdn.net/phoebin/article/details/3864590 代 ...