LeetCode——Edit Distance
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的更多相关文章
- [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
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] 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 ...
随机推荐
- Codeforces Round #426 (Div. 2)A题&&B题&&C题
A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...
- OSharp DbContent初始化分析
DBContent初始化 —— 关联Entity查找 一. 关联到具体的Entity 二. 通过EntityTypeConfiguration 关联到DbContent 三. ...
- shipyard 中文版安装 -- Docker web管理
#本文使用markdown文档格式 #Docker web管理平台 #shipyard 中文版安装 #hipyard可对容器.镜像.仓库.docker节点进行管理的web系统 #+++++++++++ ...
- oracle入门(7)——存储过程
[本文介绍] 熟悉了PL/SQL语法后,实现java调用oracle存储过程才是主要目的.本文将介绍如何写存储过程,java如何调用存储过程. [存储过程介绍] 抛开专业的描述,存储过程就是在数据库里 ...
- 前端 html body 内标签之input
可以做登录页面 text是文本输入框 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- Spark Sql的UDF和UDAF函数
Spark Sql提供了丰富的内置函数供猿友们使用,辣为何还要用户自定义函数呢?实际的业务场景可能很复杂,内置函数hold不住,所以spark sql提供了可扩展的内置函数接口:哥们,你的业务太变态了 ...
- python操作socket
Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...
- 创建Java不可变类
不可变(immutable)类的意思是创建该类的实例后,该实例的Field是不可改变的,Java提供的8个包装类和java.lang.String类都是不可变类. 如果需要创建自定义的不可变类,可遵守 ...
- Python:6种标准数据类型
原文地址https://www.cnblogs.com/qin1991/p/5910145.html #!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行 ...
- json & pickle数据序列化
序列化:把内存中的数据对象变成字符串 info = { 'name':'tom', 'age':22 } f = open("test.txt","w") f. ...