【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了

 1 public class Solution {
 2     public int minDistance(String word1, String word2) {
 3         char c1[]=word1.toCharArray();
 4         char c2[]=word2.toCharArray();
 5         int len1=word1.length();
 6         int len2=word2.length();
 7         int dp[][]=new int[len1+1][len2+1];
 8         int i,j;
 9         for(i=0;i<len2+1;i++) dp[0][i]=i;
10         for(j=0;j<len1+1;j++) dp[j][0]=j;
11         for(i=1;i<len1+1;i++)
12         {
13
14             for( j=1;j<len2+1;j++)
15             {
16                 if(c1[i-1]==c2[j-1]) dp[i][j]=dp[i-1][j-1];
17                 else{
18                     dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+1;
19
20                     dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+1);
21
22
23
24                 }
25
26
27
28
29             }
30
31
32
33         }
34
35
36         return dp[len1][len2];
37
38
39     }
40 }

【leetcode】编辑距离

时间: 2024-08-29 01:56:49

【leetcode】编辑距离的相关文章

[LeetCode] One Edit Distance 一个编辑距离

Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance的拓展,然而这道题并没有那道题难,这道题只让我们判断两个字符串的编辑距离是否为1,那么我们只需分下列三种情况来考虑就行了: 1. 两个字符串的长度之差大于1,那么直接返回False 2. 两个字符串的长度之差等于1,那么长的那个字符串去掉一个字符,剩下的应该和短的字符串相同 3. 两个字符串的长度之

编程之美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 char

[LeetCode]72. 编辑距离(DP)

题目 给定两个单词?word1 和?word2,计算出将?word1?转换成?word2 所使用的最少操作数?. 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例?1: 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 'h' 替换为 'r') rorse -> rose (删除 'r') rose -> ros (删除 'e') 来源:力

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)

LeetCode:Edit Distance(字符串编辑距离DP)

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) Repla

leetcode:72. 最小编辑距离

题目描述: 给定两个单词?word1 和?word2,计算出将?word1?转换成?word2 所使用的最少操作数?. 你可以对一个单词进行如下三种操作: 插入一个字符删除一个字符替换一个字符示例?1: 输入: word1 = "horse", word2 = "ros"输出: 3解释: horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')示例?2: 输入:

[题记-动态规划] 编辑距离 - leetcode

题目: 编辑距离 给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符删除一个字符替换一个字符 示例 1: 输入:word1 = "horse", word2 = "ros"输出:3解释:horse -> rorse (将 'h' 替换为 'r')rorse -> rose (删除 'r')rose -> ros (删除 'e')示例 2:

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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 char

[Leetcode 72]编辑距离 Edit Distance

[题目] Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1