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 ...
随机推荐
- PHP伪静态与短链接
如今,Web服务高速发展的时代,各式各类的门户网站,如新浪http://www.sina.com.腾讯http://www.qq.com,这些网站大家都很容易记住,因为这种名称都是有规则和含义的.如果 ...
- ACdream OJ 1153 (k-GCD)
题目链接: http://115.28.76.232/problem?pid=1153 题意: 从给定的n个数中取出k个数,使得他们的最大公约数最大,求这个最大的公约数 分析: 暴力分解不可取,我们能 ...
- wpf中,一个简单的自定义treeview
首先创建一个自定义控件,在里面定义好treeview的样式,将本来的三角形的图标变为加号的图标,并且添加节点之间的连线. <UserControl x:Class="TreeViewE ...
- AppSettings
1.winform中读写配置文件appSettings 一节中的配置. #region 读写配置文件 /// <summary> /// 修改配置文件中某项的值 /// </summ ...
- js判断是否为ie的方法
原文:http://blog.sina.com.cn/s/blog_7bbe4a850100v95z.html 下面第三种亲测可用 第一种: if(window.addEventListener){ ...
- Direct2D DirectWrite绘制文字
绘制文本使用DirectWrite: 为了简化 DirectWrite 的使用,RenderTarget有3个方法可以直接绘制文本: DrawText,用于简单绘制,支持Unicode. DrawTe ...
- golang Date format
package main import ( "fmt" "time" ) // @link https://golang.org/pkg/time/ func ...
- HTML5动画图片播放器 高端大气
我们见过很多图片播放插件(焦点图),很多都基于jQuery.今天介绍的HTML5图片播放器很特别,它不仅在图片间切换有过渡动画效果,而且在切换时图片中的元素也将出现动画效果,比如图中的文字移动.打散. ...
- SSO单点登录PHP简单版
前面做了一个新项目,需要用户资源可以需要共享.由于之前没有做过这样的东西,回家之后,立马网站百度"单点登录".帖子很多,甄别之后,这里列几篇认为比较有营养. http://blog ...
- 使用StackTrace堆栈跟踪记录详细日志(可获取行号)
上一篇我们提到使用.NET自带的TraceSource实现简单的日志,具体请看<轻松背后的N+疲惫——系统日志>,这一篇注意想讲的是日志的详细记录,包含请求开始到结束的过程中调用的方法链以 ...