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++类的封装_工程

    一个C++工程 main.cpp #include<stdio.h> #include"Array.h" int main(){     Array a1(10); f ...

  2. BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )

    跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...

  3. 关于MooTools你应该熟知的6个基本知识

    MooTools是一个精简.模组化同时也面向对象的JavaScript框架,它设计给中等和进阶的JavaScript开发人员使用.使用 MooTools优美.详细而条理分明的API,可让你写出强大.富 ...

  4. LNMP一键安装包sh脚本

    Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...

  5. Ecmall系统自带的分页功能使用

    在控制器如果没有定义相关模型,直接使用sql语句的话,直接使用如下语句. 即: public $db; $this->db = &db(); //然后开始使用分页类 $sql='sele ...

  6. 完全掌握KMP算法思想

    文档下载页面http://download.csdn.net/detail/yedeqixian/4209500      80页在讲KMP算法的开始先举了个例子,让我们对KMP的基本思想有了最初的认 ...

  7. perl学习(1) 入门

    Perl 被设计成90%擅长处理文本,10%处理其余的问题.因此Perl 有强大的文本处理能力,包括正则表达式. 第一个程序 hello world #! /usr/bin/perl -w use s ...

  8. Phalcon框架中的另类使用

    不像传统的PHP框架,假设框架想被还有一个框架使用仅仅能通过rpc或是引入文件等的方式.Phalcon能够在其他框架中直接使用.这是因为Phalcon是以扩展的形式存在的,在server载入时会直接载 ...

  9. iOS 开发 Message Digest Algorithm 5(MD5加密)

    MD5的全称是Message Digest Algorithm 5(消息摘要算法第五版),是计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.在90年代初由MIT Laboratory ...

  10. ST官方翻译的中文应用笔记汇总

    ST官方翻译的中文应用笔记汇总 http://www.51hei.com/stm32/3382.html 官方中文AN:AN3116:STM32? 的 ADC 模式及其应用AN1015:用于提高微控制 ...