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
class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = word1.size();
int n = word2.size();
vector<vector<int>> table(m+1,vector<int>(n+1, 0)); for(int i = 1; i <= n; i++) table[0][i] = i;
for(int i = 1; i <= m; i++) table[i][0] = i;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n;j++)
{
int min1 = table[i-1][j] < table[i][j-1] ? table[i-1][j] : table[i][j-1];
min1++;
int min2 = word1[i-1] == word2[j-1] ? 0 : 1;
min2 += table[i-1][j-1];
table[i][j] = min1 < min2 ? min1 : min2;
} return table[m][n];
}
};
reference :http://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/
LeetCode_Edit Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离
You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- JAVA面向对象总结
面向对象概述 面向对象是当前计算机界关心的重点,它是90年代软件开发方法的主流.面向对象的概念和应用已超越程序设计和软件开发,如数据库系统.交互式界面.应用结构.应用平台.分布式系统.网络 ...
- HDFS文件系统的操作
package com.bank.utils; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;impor ...
- oracle修改服务器端编码
因为装的是oracle 11g免费版,没有装oracle客户端,然后从网上找了免客户端使用pl/sql的教程,具体可以看链接,这里不再累述:但打开pl/sql的时候提示客户端和服务端编码不一致:网上一 ...
- http常见错误
100:继续 客户端应当继续发送请求.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应. 101: 转换协议 在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消 ...
- ionic框架前端生产环境的简单部署
1. 效果对比 1.1 开发环境 css+js+lib文件大小为好多M :) 1.2 部署环境(生产环境) css+js+lib文件大小约为800K 文件大小:好多M–>800K(多少自己试下) ...
- CMA连续物理内存用户空间映射---(一)
背景: 在多媒体和图像处理等应用中,经经常使用到大块内存,尤其是硬件编解码.须要内核分配大块的物理连续内存. 这里希望通过把从内核分配的连续物理内存映射到用户空间.在用户空间经过处理,又能够入队到驱动 ...
- openstack单元測试用组件一览
声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 组件一览 hacking 一 ...
- Vim的设置和使用——编程者
一.第一个插件:Ctags 当我们看到一个陌生的变量或者函数,我们总想知道它的含义,因此,快速找到它的定义很重要.Ctags插件中的"Ctrl+]"快捷键就可以做到. 二.教你高效 ...
- SUN-LDAP6.3_RHEL 5.0-卸载LDAP
卸载LDAP 1.注销服务器 到目录/ldap/ldapinstance/dscc6/bin下 # ./dsccreg remove-server -h 主机名 /ldap/ldapinstance/ ...
- DownloadManager 下载管理类
演示 简介 从Android 2.3开始新增了一个下载管理类,在SDK的文档中我们查找android.app.DownloadManager可以看到.下载管理类可以长期处理多个HTTP下载任务,客户端 ...