[leetcode] 72. Edit Distance (hard)
原题
dp
利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步。(每一次删增改都算1步)
所以可得边界状态dp[i][0]=i,dp[0][j]=j。
以及状态转移方程
即当比较 word1[i] 和 word2[j] 字符 相等 时,所需步数与 word1[i-1] 和 word2[j-1] 相等。
状态转移方程为:dp[i][j]=dp[i-1][j-1]
否则,状态转移方程为dp[i][j]= min(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])+1
class Solution
{
public:
int minDistance(string word1, string word2)
{
int oneSize = word1.size() + 1;
int twoSize = word2.size() + 1;
int dp[oneSize][twoSize] = {0};
for (int i = 0; i < oneSize; i++)
dp[i][0] = i;
for (int j = 0; j < twoSize; j++)
dp[0][j] = j;
for (int i = 1; i < oneSize; i++)
{
for (int j = 1; j < twoSize; j++)
{
int temp;
if (word1[i - 1] == word2[j - 1])
{
dp[i][j] = dp[i - 1][j - 1];
}
else
{
temp = min(dp[i - 1][j], dp[i][j - 1]);
dp[i][j] = min(dp[i - 1][j - 1], temp) + 1;
}
}
}
return dp[oneSize - 1][twoSize - 1];
}
};
[leetcode] 72. Edit Distance (hard)的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 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]72. Edit Distance 最少编辑步数
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
随机推荐
- 新兴技术袭来,Web开发如何抉择?
土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/ 使用HTML 创建Mac OS App 视频教程. 官方QQ群: (1)App实践出真知 434 ...
- qt的demo中,经常可以看到emum
最近开始看QT的文档,发现了很多好东西,至少对于我来说 收获很多~~~ 当然很多东西自己还不能理解的很透彻,也是和朋友讨论以后才渐渐清晰的,可能对于QT中一些经典的用意我还是存在会有些认识上的偏差,欢 ...
- Oracle 裁掉北京研发团队,相应职位撤回美国(收购了NetSuite,LogFire,Dyn)
根据中国日报报道,2017年1月14日上午9点09分,甲骨文北京研发团队的同事收到了来自BU老大的一封邮件.邮件上提及,由于市场变化,甲骨文开始整合各研发中心资源公司在云计算方向发力,文末单独提出了甲 ...
- iOS11中iOS处理GIF图片的方式
GIF 五部走如下 : 1 从相册中取出GIF图的Data 2 通过腾讯的IM发送Gif图 3 展示GIF图 4 GIF图URL缓存机制 5 将展示的GIF图存到相册中 一 从相册中 ...
- HTML续
HTML class属性 定义和用法 class 属性规定元素的类名(classname). class 属性大多数时候用于指向样式表中的类(class).不过,也可以利用它通过 JavaScript ...
- github 上传更新代码(最简单的方法)
1.首先你需要一个github账号,还没有的朋友先去注册一个吧! GitHub地址:https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可 ...
- ZooKeeper —— 单机环境和集群环境搭建
一.单机环境搭建 1.1 下载 下载对应版本Zookeeper,这里我下载的版本3.4.14.官方下载地址:https://archive.apache.org/dist/zookeeper/ # w ...
- 戏说 .NET GDI+系列学习教程(一、Graphics类--纸)
Graphics类(纸) Graphics类封装一个GDI+绘图图面,提供将对象绘制到显示设备的方法,Graphics与特定的设备上下文关联. 画图方法都被包括在Graphics类中,在画任何对象时, ...
- 【Linux杂记】Linux配置静态IP地址,修改主机名、host
博主使用的系统是:乌班图16.04 1.设置静态IP方法如下: #sudo vim /etc/network/interfaces #修改如下部分: auto eth0//ipconfig命令查看网卡 ...
- python读取excel文件中所有sheet表格
sales: store: """(1)用load_workbook函数打开excel文件,返回一个工作簿对象 (2)用工作簿对象获取所有的sheet (3)第一个for ...