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. (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中

    本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...

  2. TCP/IP网络编程之I/O流分离

    分离I/O流 “分离I/O流”是一种常用表达,有I/O工具可以区分二者.无论使用何种办法,都可以认为分离I/O流.我们之前通过两种方法分离I/O流,第一种是TCP/IP网络编程之进程间通信中的“TCP ...

  3. django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x97\\xAE\\xE9\\xA2\\x98' for column 'name' at row 5")

    django 连接MYSQL时,数据迁移时报:django.db.utils.InternalError: (1366, "Incorrect string value: '\\xE9\\x ...

  4. 03018_监听器Listener

    1.什么是监听器? (1)监听器就是监听某个对象的状态变化的组件: (2)监听器的相关概念 ①事件源:被监听的对象------三个域对象:request.session.ServletContext ...

  5. 【SCOI 2010】股票交易

    题目 最近 \(\text{lxhgww}\) 又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,\(\text{lxhgww}\) 预测到了未来 \(T ...

  6. Leetcode 546.移除盒子

    移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k > ...

  7. redis命令monitor详解

    通过monitor这个命令可以查看数据库在当前做了什么操作,对于管理redis数据库有这很大的帮助 如图示,在redis客户端进行操作显示info,另一个窗口打开monitor就会显示出这个命令的操作 ...

  8. 为什么对多线程编程这么怕?pthread,sem,mutex,process

    转自http://blog.chinaunix.net/uid-20788636-id-1841334.html 1.线程创建和退出创建线程实际上就是确定调用该线程函数的入口点,这里通常使用的函数是p ...

  9. java中使用二进制进行权限控制

    基本概念 package test; publicclass Rights { publicstaticvoid main(String[] args) { int a=1; // 001 状态a i ...

  10. IO Streams:数据流

    数据流支持原始数据类型值(布尔型,字符型,字节型,短型,长整型,浮点型和双倍型)的二进制I / O以及字符串值.所有数据流都实现了DataInput接口或DataOutput接口.本节重点介绍这些接口 ...