[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 ...
随机推荐
- Delphi 屏幕抓图技术的实现
摘 要:本文以Delphi7.0作为开发平台,给出了网络监控软件中的两种屏幕抓图技术的设计方法和步骤.介绍了教师在计算机机房内教学时,如何监控学生计算机显示器上的画面,以保证教学的质量和效果. 引言 ...
- 预处理器#include 指令
预处理器发现 #include 指令后,就会寻找后跟的文件名并把这个文件的内容包含到当前文件中.被包含文件中的文本将替换源代码文件中的#include指令,就像你把被包含文件中的全部内容键入到源文件中 ...
- WCF研究-中篇
中篇 5.托管于宿主 6.消息模式 7.WCF行为-实例管理和并发控制 8.安全 5.托管于宿主 托管 宿主Host Ø承载WCF Service运行的环境 自承载方式 系统服务方式 IIS方式 WA ...
- 更改当前电源策略(使用SetActivePwrScheme API函数),自定义电源按钮动作(设置GLOBAL_POWER_POLICY)
#include <windows.h> #include <Powrprof.h> #pragma comment(lib, "Powrprof.lib" ...
- QImage的浅拷贝与深拷贝
首先简单说说什么是浅拷贝和深拷贝:浅拷贝就比如像引用类型,而深拷贝就比如值类型,即浅拷贝是共用一块内存的,而深拷贝是复制一份内容. 我们再来看看QImage类的几个构造函数: // 浅拷贝 QI ...
- 使用AnimateWindow来实现窗口淡入淡出(主要有四种动画,滚动,滑动,折叠或展开,和淡入淡出)
如果是在VC6下进行编译,应引入下面的预编译宏,注意放在windows.h的前面#undef WINVER #define WINVER 0x500为什么要引入上面的宏呢?看看winuse ...
- kubernetes实战篇之nexus oss服务器部署及基于nexus的docker镜像仓库搭建
系列目录 Nexus oss仓库管理平台搭建 Nexus是一款仓库管理工具,支持Npm,bower,maven,nuget,apt,yum甚至docker,helm等各种仓库,说的通俗以下,就是私服镜 ...
- redis在asp.net 中的应用
1.redis介绍 Nosql数据库作为关系型数据库的补充,在互联网公司已经得到广泛的运用.redis便是其中的代表之一,redis是一种(key,value)基于内存的数据库,并支持多种数据结构,如 ...
- Jquery实现搜索功能
<script> //搜索功能 (function ($) { jQuery.expr[':'].Contains = function (a, i, m) { return (a.tex ...
- shell写的俄罗斯方块
共享一下. #!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaratio ...