edit distance leetcode
这样的字符转换的dp挺经典的,
class Solution {
public:
int minDistance(string word1, string word2) {
int dp[word1.size()+1][word2.size()+1];
for(int i = 0; i < word1.size()+1; i++)
dp[i][0] = i;
for(int i = 0; i < word2.size()+1; i++)
dp[0][i] = i;
for(int i = 0; i < word1.size(); i++) {
for(int j = 0; j < word2.size(); j++) {
if(word1[i] == word2[j])
dp[i+1][j+1] = dp[i][j];
else
dp[i+1][j+1] = min(min(dp[i][j],dp[i][j+1]),dp[i+1][j])+1;
}
}
return dp[word1.size()][word2.size()];
}
};
edit distance leetcode的更多相关文章
- Edit Distance leetcode java
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- 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 ...
- [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 ...
- LeetCode One Edit Distance
原题链接在这里:https://leetcode.com/problems/one-edit-distance/ Given two strings S and T, determine if the ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week8]Edit Distance
Edit Distance 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/edit-distance/description/ Description ...
随机推荐
- XMind快捷键可以自定义吗
在使用快捷键的时候,不知你是否有过这样的疑问,为什么这个操作的快捷键一定要是这个呢,我为什么不能换成其他的按键呢.其实这些在XMind思维导图中都是可以更改的,用户可以根据自己的操作习惯来定义快捷键命 ...
- 认识Junit
JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己的JUn ...
- sharepreference实现记住password功能
SharePreference是用于保存数据用的.主要调用Context.getSharePreferences(String name, int mode)方法来得到SharePrefere ...
- 自定义视图控制器切换(iOS)
在iOS开发过程中,通常我们会使用UINavigationController,UITabbarController等苹果提供的视图控制器来切换我们的视图.在iOS5之前,如果要自定义容器视图控制器很 ...
- for循环、穷举法和迭代
循环:初始条件,循环条件,状态改变,循环体.for(初始条件;循环条件;状态改变){ 循环体}for(int i=1;i<=10;i++){ }例子:100以内与7有关的数.求100以内所有数的 ...
- SQL创建登陆用户和赋予权限
主要针对Sql server 2005及以上,创建简单用户名和密码所引起的密码简单的问题.解决方案 CHECK_POLICY = OFF; --强制密码策略 use MusicStore --创建登陆 ...
- SonarQube 项目配置文件
费话不说,直接上代码: 需要注意的地方: 1. 每个项目的key不能重复. 2. 注意编码方式. 3. 注意分模块的写法. 4. 忽略源码文件的写法. # Required metadatasonar ...
- pl sql练习(1)
编写函数接受参数并返回字符串:Hello $var.然后赋值给绑定变量并打印: create or replace function hello_function ( pv_whom varchar2 ...
- 使用FMDB,libqrencode实现二维码的生成并且保存到数据库
/** * 1.首先导入第三方库FMDB和libqrencode,添加库libsqlite3.tbd
- c#&.NET3.0高级程序设计-02 Enum Demo
Enum 实例 using System; using System.Collections.Generic; public class MyClass { enum EmpType ...