Leetcode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
输入两个单词word1和word2,求出从word1转换成word2的最少步骤。每个转换操作算一步。转换操作限定于:
- 删除一个字符
- 插入一个字符
- 替换一个字符
本题用动态规划求解
设决策变量dp[i][j],表示从word[0..i-1]转换为word2[0...j-1]的最少步骤

可以参考http://web.stanford.edu/class/cs124/lec/med.pdf
class Solution {
public:
int minDistance(string word1, string word2) {
int len1 =word1.length(), len2 = word2.length();
if(len1 == ) return len2;
if(len2 == ) return len1;
if(word1 == word2) return ;
vector<vector<int> > dp(len1+, vector<int>(len2+,));
for(int i = ; i <= len1; ++ i) dp[i][] = i;
for(int j = ; j <= len2; ++ j) dp[][j] = j;
for(int i =; i <= len1; ++ i){
for(int j = ; j <= len2; ++ j){
dp[i][j] = min(dp[i-][j-]+(word1[i-] != word2[j-]? : ),min(dp[i-][j]+, dp[i][j-]+));
}
}
return dp[len1][len2];
}
};
Leetcode Edit Distance的更多相关文章
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:Edit Distance 解题报告
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [leetcode]Edit Distance @ Python
原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...
- [LeetCode] Edit Distance 字符串变换为另一字符串动态规划
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode: Edit Distance && 子序列题集
Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...
- LeetCode——Edit Distance
Question Given two words word1 and word2, find the minimum number of steps required to convert word1 ...
- [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 ...
随机推荐
- [Unity3D]粒子系统学习笔记
粒子阴影的处理 通过Material填充粒子系统的render后,默认是显示阴影的: 可以通过设置来调整: 调整后的效果, 每个粒子就没有阴影了 增加粒子效果 设置为合成的材质,效果显示加倍: 添加子 ...
- git操作---查询
1.查看git的状态 git status 2.查看git的日志历史记录 git log 3.查看当前git的分支 git branch 4.查看git的配置信息 git config --lis ...
- 在Application中集成Microsoft Translator服务之开发前准备
第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...
- 基于 BinaryReader 的高效切割TXT文件
日常工作中免不了要面对一些文件的操作.. 但是如果是日志文件..动辄上G的..处理起来就不那么轻松随意了.. 尤其文件还很多的时候.. 这个时候就会用到大文件切割.. 下边贴出的示例是实验了一个 10 ...
- 手动封装js原生XMLHttprequest异步请求
Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...
- C和指针 第九章 习题
9.15 编写函数格式化金钱为标准字符串 #include <stdio.h> #include <string.h> #define TEMP_LEN 1000 void d ...
- word20161213
journal queue / 日志队列 journal quota / 日志配额 junction point / 交叉点 KDC, Key Distribution Center / 密钥分发中心 ...
- 线性SVM
(本文内容和图片来自林轩田老师<机器学习技法>) 1. 线性SVM的推导 1.1 形象理解为什么要使用间隔最大化 容忍更多的测量误差,更加的robust.间隔越大,噪声容忍度越大: 1.2 ...
- 编译Android源码
编译版本要求 基本安装环境 ubuntu 14.04 64 sudo apt-get install git-core gnupg flex bison gperf build-essential \ ...
- jquery--常用的函数2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...