Question

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

Solution

动态规划。参见以前的博客:http://www.cnblogs.com/zhonghuasong/p/6147030.html

Code

class Solution {
public:
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
for (int i = 0; i <= len1; i++)
dp[i][0] = i;
for (int j = 0; j <= len2; j++)
dp[0][j] = j;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
int tmp = word1[i - 1] == word2[j - 1] ? 0 : 1;
dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + tmp));
}
}
return dp[len1][len2];
}
};

LeetCode——Edit Distance的更多相关文章

  1. [LeetCode] Edit Distance 编辑距离

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

  2. Leetcode:Edit Distance 解题报告

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

  3. [leetcode]Edit Distance @ Python

    原题地址:https://oj.leetcode.com/problems/edit-distance/ 题意: Given two words word1 and word2, find the m ...

  4. [LeetCode] Edit Distance 字符串变换为另一字符串动态规划

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

  5. Leetcode Edit Distance

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

  6. [LeetCode] Edit Distance(很好的DP)

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

  7. LeetCode: Edit Distance && 子序列题集

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

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

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

  9. 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 ...

随机推荐

  1. Apache Kafka源码分析 – ReplicaManager

    如果说controller作为master,负责全局的事情,比如选取leader,reassignment等那么ReplicaManager就是worker,负责完成replica的管理工作 主要工作 ...

  2. d3.js 之SVG:矢量化图形绘制

    SVG Scalable Vector Graphics 是一个成熟的W3C标准,被设计用来在web和移动平台 上展示可交互的图形.和HTML类似,SVG也支持CSS和JavaScript.尽管可以使 ...

  3. Ponds----hdu5438(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438   题意:有n个池塘和m个管道:每个池塘的价值是v, 现在由于资金问题要删除池塘:但是删除的池塘 ...

  4. ubuntu安装mysql步骤

    https://dev.mysql.com/downloads/file/?id=477124 ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get inst ...

  5. Flask的配置与路由

    配置管理 flask中的配置文件是一个flask.config.Config对象(继承字典),默认配置为:    {        'DEBUG':                    get_de ...

  6. maven之jre默认配置

    需要在用户或者全局settings.xml中做如下配置 例:用户配置: 添加代码: <profile> <id>jdk-1.8</id> <activatio ...

  7. centos删除乱码名称的文件

    常规方法rm已经木有办法删除该文件了. 原理: 当文件名为乱码的时候,无法通过键盘输入文件名,所以在终端下就不能直接利用rm,mv等命令管理文件了.但是每个文件都有一个i节点号,可以通过i节点号来管理 ...

  8. MySQL整理(三)

     一.简单单表操作   (1)简单CRUD 插入查询结果 insert into table1(id,name,age) select id,name,age from table2 where id ...

  9. MongoDB主从复制+集群

    一.读写分离的概念 读写分离,基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELETE),而从数据库处理SELECT查询操作.数据库复制被用来把事务性操作导致的变更同步 ...

  10. vultr服务器上搭建PHP运行环境-centos7

    安装nginx: yum install nginx 开启Nginx systemctl start nginx 访问你的域名或者ip 例如:quantaedu.com 安装PHP 查看系统版本lsb ...