[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 ...
随机推荐
- qlineedit设置背景颜色(使用QPalette的方法不行,必须使用QSS)
使用QPalette的方法不行, ui->le_text->setAutoFillBackground(true);qDebug() << ui->le_text-> ...
- C#调用记事本并填写内容
using System.Runtime.InteropServices; using System.Diagnostics; [DllImport("User32.DLL") ...
- x64内联汇编调用API(需intel编译器,vc不支持x64内联汇编)
#include "stdafx.h" #include <windows.h> STARTUPINFOW StartInfo = {0}; PROCESS_INFO ...
- Codility----PassingCars
Task description A non-empty zero-indexed array A consisting of N integers is given. The consecutive ...
- Spring Cloud Ribbon配置详解
概述 有时候需要自定义Ribbon的配置和客户端超时配置. 自动化配置 /* 使用属性自定义功能区客户端 从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文 ...
- 04 body中的相关标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Vue 牛刀小试]:第十四章 - 编程式导航与实现组件与 Vue Router 之间的解耦
一.前言 在上一章的学习中,通过举例说明,我们了解了 Vue Router 中命名路由.命名视图的使用方法,以及如何通过 query 查询参数传参,或者是采用 param 传参的方式实现路由间的参数传 ...
- composer-laravel-China源和官方源
composer config -g repo.packagist composer https://repo.packagist.org composer config -g repo.packag ...
- solr 重要的知识点
1 solr 查询参数说明 常用 ) q - 查询字符串,必须的. ) fl - 指定返回那些字段内容,用逗号或空格分隔多个. ) start - 返回第一条记录在完整找到结果中的偏移位置, 开始,一 ...
- 前端页面统计beacon调研
目录 为什么使用beacon beacon特性 beacon 示例 参考资料 主要用于测试html的新特性beacon,使用beacon向后端发送请求,代替xhr或jsonp, 好处是支持页面unlo ...