【leetcode】编辑距离
dp方程“
1、初始化;dp[0][i]=i; dp[j][0]=j;
2.dp[i][j]= dp[i-1][j-1](相等)
dp[i-1][j]+1 ,,dp[i][j-1]+1; dp[i-1][j-1] (这个对应是改的况)
注意字符串下标开始位置就OK了
public class Solution {
public int minDistance(String word1, String word2) {
char c1[]=word1.toCharArray();
char c2[]=word2.toCharArray();
int len1=word1.length();
int len2=word2.length();
int dp[][]=new int[len1+1][len2+1];
int i,j;
for(i=0;i<len2+1;i++) dp[0][i]=i;
for(j=0;j<len1+1;j++) dp[j][0]=j;
for(i=1;i<len1+1;i++)
{
for( j=1;j<len2+1;j++)
{
if(c1[i-1]==c2[j-1]) dp[i][j]=dp[i-1][j-1];
else{
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+1;
dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+1);
}
}
}
return dp[len1][len2];
}
}
【leetcode】编辑距离的更多相关文章
- leetcode 编辑距离
class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...
- LeetCode 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [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 ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
- [LeetCode]72. 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...
- [leetcode] 72. 编辑距离(二维动态规划)
72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- top工具
top 显示进程所占系统资源 能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top命令打印出了很多信息,包括系统负载(loadaverage).进程数(Tasks).c ...
- 自己写的访问SqlServer数据库的通用DAL层
如题,直接贴代码. 首先是DataTable转List<T>的方法,这个方法通用性极强. #region Table转List /// <summary> /// Table转 ...
- SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events
由于工作需要,决定深入研究SQL Server的扩展事件(Extended Events/xEvents),经过资料搜索,发现国外大牛的系列文章,作为“学习”阶段,我先翻译这系列文章,后续在工作中的心 ...
- html标签data大写获取不到值:只能小写+横杠命名
html标签data大写获取不到值:只能小写+横杠命名 例如: <i class="glyphicon glyphicon-question-sign" data-tip-t ...
- 在Django中使用Mako模板
用了一个月后,终于忍受不了Django的模板了.主要原因是模板内不能运行原生的python语句,所以用起来总感觉被人绑住手脚,遍历个字典都要搞半天. 所以决定用第三方的模板.查了一下,django用的 ...
- Controllers, Actions 和 Action Results
Controllers, Actions 和 Action Results 原文:Controllers, Actions, and Action Results作者:Steve Smith翻译:姚阿 ...
- uva 12206 - Stammering Aliens
基于hash的LCP算法: #include<cstdio> #include<cstring> #include<algorithm> #define maxn ...
- underscore demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- forward:hello 与 redirect:hello的区别
对于某些Controller的处理方法,当返回值为String类型时,返回的结果中可能含有forward或redirect前缀: 如: @Controller @RequestMapping(&quo ...
- 移动应用产品开发-android开发(一)
最近公司希望增添移动开发业务,进行移动互联网开发的调研及产品需求调研. 我主要负责技术解决方案的研究,从android开发开始学习.同时跟经理一起与其他部门同事沟通了解移动开发方面的需求. 在了解an ...