class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row=word1.length();
int col=word2.length();
vector< vector<int> > dis(row+1,vector<int>(col+1,0));
dis[0][0]=0;
for(int i=1;i<row+1;i++){
dis[i][0]=i;
}
for(int j=1;j<col+1;j++){
dis[0][j]=j;
}
for(int i=1;i<row+1;i++){
for(int j=1;j<col+1;j++){
int mindis=min(dis[i-1][j]+1,dis[i][j-1]+1);
assert(mindis>=1);
dis[i][j]=min(mindis,dis[i-1][j-1]+getDis(word1[i-1],word2[j-1]));
}
}
return dis[row][col];
}
private:
int getDis(char a,char b){
if(a==b)
return 0;
else
return 1; }
};

难点:dis数组中row col的下标代表的是string中位置为i-1的字符串

例如:dis【1】【1】为word1【0】到word2【0】的编辑距离。

leetcode 编辑距离的更多相关文章

  1. LeetCode 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...

  2. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  3. [LeetCode] 161. One Edit Distance 一个编辑距离

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  4. [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)

    https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...

  5. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

  6. [LeetCode]72. 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...

  7. [leetcode] 72. 编辑距离(二维动态规划)

    72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...

  8. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  9. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

随机推荐

  1. 一个简单二叉树的C++实现(一)

    很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用. /* * BinaryTree.cpp * Author: Qiang Xiao * Time: 2015-07- ...

  2. C#变量命名规范

    1.1命名 1.  所有命名必须有意义 2.  成员变量声明在类的顶端,并且每个变量一行 3.  局部变量声明在引用之前 1.1.1  常量命名 1.  常量名用全大写:MAX_PARAMETER_C ...

  3. HTML5 总结-视频-1

    HTML5 视频 视频格式 当前,video 元素支持三种视频格式: 格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG ...

  4. IT资源专业搜索-www.easysoo.cn

    创始人:samrthhl 时间:2015-11-8 关于易搜 易搜(www.easysoo.cn)是一个面向IT开发从业人员的专业资源搜索站点,它将全球的知名博客论坛.专业IT行业站点.知名咨询机构和 ...

  5. 我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)

    一.面向对象基础 面向对象名词解释: 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公 ...

  6. hdoj 1013

    比较坑的一道题...直接做肯定WA http://acm.hdu.edu.cn/showproblem.php?pid=1013 #include<iostream> #include&l ...

  7. poj 3984 迷宫问题(dfs)

    题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...

  8. 6.基于ZMQ的游戏网络层基础架构

    对于内网服务器的通信采用zmq来进行,对于和客户端的通信采用boost的asio来.这里先来搭建zmq的基础结构. zmq相关的知识可以去zmq官方网站查询. 这里使用zmq的push 和pull来进 ...

  9. error C2664: “LoadLibraryW”: 不能将参数 1 从“const char *”转换为“LPCWSTR”

    在使用VS2010编写运行时动态链接dll文件时出现的一个问题,问题解决得益于此文章: http://blog.sina.com.cn/s/blog_6a2236590100xbgl.html 通过调 ...

  10. 使用Sphinx生成静态网页

    转载来自 http://www.ibm.com/developerworks/cn/opensource/os-sphinx-documentation/ 简介 Sphinx 是一种工具,它允许开发人 ...