https://oj.leetcode.com/problems/edit-distance/

动态规划,它的小规模问题是:当 word1  word2都比较小的时候,word1变成 word2需要的变化数。

设数组 record[i][j] 表示word1 从0到 i ,word2 从 0 到 j 的 word1 到 word2 需要经过几次变化。

列等式如下:

record[i][j] = record[i-1][j-1]   if 'i'=='j'

record[i][j] = record[i-1][j-1] +1  replace                     if 'i'!='j'

                  record[i][j-1] +1     delete

                 record[i-1][j] +1      insert

取它们3个的最小值。

初始化: record[0][j] = j

         record[i][0]  = i

class Solution {
public:
int minDistance(string word1, string word2) {
//word1 is smaller
if(word1.size()>word2.size())
{
string temp;
temp = word1; word1 = word2; word2 = temp;
}
if(word1.empty())
return word2.size();
if(word1 == word2)
return ; //find the max same bits
int maxSame = sameBits(word1,word2); return maxSame;
}
int sameBits(string &word1,string &word2)
{
vector<vector<int> > record;
record.resize(word1.size()+);
for(int i = ; i<= word1.size();i++)
record[i].resize(word2.size()+); for(int i = ;i<=word1.size();i++)
record[i][] = i;
for(int j = ;j<=word2.size();j++)
record[][j] = j; for(int row = ;row<= word1.size();row++)
for(int col = ;col<=word2.size();col++)
{
if(word1[row-] == word2[col-])
record[row][col] = record[row-][col-];
else
{
int _min = min(record[row-][col],record[row][col-]);
record[row][col] = min(_min,record[row-][col-]) + ;
}
} return record[word1.size()][word2.size()];
}
};
int main()
{
class Solution mys;
string str1 = "distance";
string str2 = "springbok";
cout<<mys.minDistance(str1,str2);
}

可以 const size_t lenword1 = word1.size()

      const size_t lenword2 = word2.size()

int  record[lenword1+1][lenword2+1];

这样定义数组

LeetCode OJ-- Edit Distance **的更多相关文章

  1. [LeetCode] 72. Edit Distance 编辑距离

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

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

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

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

  4. LeetCode One Edit Distance

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

  5. [Leetcode Week8]Edit Distance

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

  6. 【leetcode】Edit Distance

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

  7. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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

  8. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  9. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  10. 【leetcode】Edit Distance (hard)

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

随机推荐

  1. May I see you again?【我可以再见到你吗?】

    May I see you again "May I see you again?" he asked. There was an endearing nervousness in ...

  2. ZOJ 3231 Apple Transportation 树DP

    一.前言 红书上面推荐的题目,在138页,提到了关键部分的题解,但是实际上他没提到的还有若干不太好实现的地方.尤其是在这道题是大家都拿网络流玩弄的大背景下,这个代码打不出来就相当的揪心了..最后在牛客 ...

  3. JVM垃圾回收原理

    原文地址:http://chenchendefeng.iteye.com/blog/455883 一.相关概念 基本回收算法 1. 引用计数(Reference Counting) 比较古老的回收算法 ...

  4. Hadoop架构的初略总结(2)

    Hadoop架构的初略总结(2) 回顾一下前文,我们总结了以下几个方面.我们为什么需要Hadoop:Hadoop2.0生态系统的构成:Hadoop1.0中HDFS和MapReduce的结构模型. 我们 ...

  5. [oldboy-django][2深入django]ORM操作

    推荐学习博客:http://www.cnblogs.com/wupeiqi/articles/6216618.html 需求: 汇总django orm操作,代替原生mysql语句来操作数据库:里面内 ...

  6. TOJ 3974: Region n条直线m个圆最多将圆分为几个区域

    3974: Region  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 33     ...

  7. var和function定义方法的区别

    在JS中有两种定义函数的方式,1是var aaa=function(){...}2是function aaa(){...}var 方式定义的函数,不能先调用函数,后声明,只能先声明函数,然后调用.fu ...

  8. sql server 韩文查询匹配失败

    在SQL Server 中查询韩文信息时,没有匹配到对应的信息,检查程序后发现字段类型是nvarchar类型的没有问题, 打开存储过程后找到问题了:原来是拼接后的查询语句存储在一个varchar变量中 ...

  9. Vue2.0 - 构造器的延伸 Vue.extend

    Vue.extend 返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue实例构造器.经常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件名称作为标签的自定义元 ...

  10. Log4j官方文档翻译(五、日志输出的方法)

    日志类提供了很多方法用于处理日志活动,它不允许我们自己实例化一个logger,但是提供给我们两种静态方法获得logger对象: public static Logger getRootLogger() ...