LeetCode OJ-- Edit Distance **
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 **的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- 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 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- GoF23种设计模式之创建型模式之抽象工厂模式
一.概述 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 二.适用性 1.一个系统要独立于它的产品的创建.组合和表示的时候. 2.一个系统要由多个产品系列中的一个来配置的时候. ...
- Windows Server 2008 正式版下载汇总
windows 2008是微软推出的新一代服务器专用系统版本, 具有良好的用户体验以及应用程序,windows 2008大幅提升了web服务以及应用程序的性能, 让企业在提供和维护资源服务的时候更加得 ...
- 51NOD 1128正整数分组V2 二分答案
这道题是典型的二分答案法.但是首先难道这道题的时候我进行了一系列的思考,甚至联想到了之前多校中类似于树状划分的问题...原因是大家都包括N各节点K个输入.. 实际上最开始联想到了应当使用二分法“枚举” ...
- easyui的layout
1.浏览器自适应(即浏览器改变大小,里面的表格大小也会随之改变)要设置两个参数 (1)一般都要在body上设置class=“easyui-layout”: <body class="e ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- hnust 可口可乐大促销
问题 B: 可口可乐大促销 时间限制: 1 Sec 内存限制: 128 MB提交: 653 解决: 323[提交][状态][讨论版] 题目描述 最近可口可乐在搞大促销活动,1瓶可乐只要1元钱.而且 ...
- 实战项目——获取图片中的GPS位置信息和拍摄时间
今天突然看到有人写过获取图片中位置信息的程序.我觉得很有趣,也就自己实践了一下,研究了一下 话不多说,先上代码 #!/usr/bin/env python3 # -*- coding: utf-8 - ...
- md5 加密算法和升级
在这里插一小节加密的吧,使用openssl库进行加密. 使用MD5加密 我们以一个字符串为例,新建一个文件filename.txt,在文件内写入hello ,然后在Linux下可以使用命令md5sum ...
- pip更新产生的问题及其解决方法?
运行 pip3 install --upgrade pip 发生错误: from pip import main ImportError: cannot import name 'main' 将以下代 ...
- windows server 2012 下IIS8.5关于“ 配置错误 不能在此路径中使用此配置节”的解决办法
服务器升级为windows server 2012 r2后,发布在新装的IIS8.5上的网站不能访问,页面显示“500 - 内部服务器错误.” 在服务器上调试后,提示的错误信息为: 配置错误 不能在此 ...