leetcode 72. 编辑距离

/*****
定义状态:
DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符
DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数;
状态转移:
for i:[0,m]
    for j:[0,n]
    if(word1[i-1]==word2[j-1])
        DP[i][j]=DP[i-1][j-1];
    else
        DP[i][j]=min(DP[i-1][j],DP[i][j-1],DP[i-1][j-1])+1;
return DP[m][n];

******/

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m=word1.size(),n=word2.size();
        vector<vector<int> > DP(m+1,vector(n+1,0));
        //初始化
        for(int i=0;i<=m;i++){
            DP[i][0]=i;
        }
        for(int j=0;j<=n;j++){
            DP[0][j]=j;
        }
        //状态转移
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(word1[i-1]==word2[j-1])
                    DP[i][j]=DP[i-1][j-1];
                else
                    DP[i][j]=min(min(DP[i-1][j],DP[i][j-1]),DP[i-1][j-1])+1;
            }
        }
        return DP[m][n];
    }
};

原文地址:https://www.cnblogs.com/joelwang/p/10875673.html

时间: 2024-11-07 23:50:11

leetcode 72. 编辑距离的相关文章

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

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

[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

编程之美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】编辑距离

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.toCharArr

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 Java

72.Edit Distance(编辑距离) 题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路: 多次选择试图得到最优解,那么考虑动态规划. 先假设word1有len1位,word2有len2位,建立数组step,step[i][j]就代表我们要将word1前 i 位转换为word2前 j 位的最少数量. 此时word1查找到第 i+1 位字母a,

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

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

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

19.2.13 [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 = "h