zoukankan      html  css  js  c++  java
  • 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:

    1. Insert a character
    2. Delete a character
    3. Replace a character

    Example 1:

    Input: word1 = "horse", word2 = "ros"
    Output: 3
    Explanation: 
    horse -> rorse (replace 'h' with 'r')
    rorse -> rose (remove 'r')
    rose -> ros (remove 'e')
    

    Example 2:

    Input: word1 = "intention", word2 = "execution"
    Output: 5
    Explanation: 
    intention -> inention (remove 't')
    inention -> enention (replace 'i' with 'e')
    enention -> exention (replace 'n' with 'x')
    exention -> exection (replace 'n' with 'c')
    exection -> execution (insert 'u')

    题意

    求最小编辑距离

    有3个操作:删除,插入和替换

    题解

    一开始想了个dp,不够快

     1 class Solution {
     2 public:
     3     int minDistance(string word1, string word2) {
     4         int l1 = word1.length(), l2 = word2.length();
     5         vector<vector<int>>dp(l1+1, vector<int>(l2+1, INT_MAX));
     6         for (int i = 0; i <= l1; i++)dp[i][0] = i;
     7         for (int i = 0; i <= l2; i++)dp[0][i] = i;
     8         for (int i = 1; i <= l1; i++) {
     9             int minnum = INT_MAX;
    10             for (int j = 1; j <= l2; j++) {
    11                 if (word1[i-1] == word2[j-1])
    12                     dp[i][j] =  dp[i - 1][j - 1];
    13                 else {
    14                     int dp1 = dp[i - 1][j] + 1;
    15                     int dp2 = dp[i - 1][j - 1] + 1;
    16                     dp[i][j] = min(dp1, dp2);
    17                     if (minnum != INT_MAX)
    18                         dp[i][j] = min(minnum + j, dp[i][j]);
    19                 }
    20                 minnum = min(dp[i][j] - j, minnum);
    21             }
    22         }
    23         return dp[l1][l2];
    24     }
    25 };
    View Code

    其实是有个地方我没注意到: dp[i][j] 与 dp[i][j-1] 的关系实际上是映射着插入操作的

     1 class Solution {
     2 public:
     3     int minDistance(string word1, string word2) {
     4         int l1 = word1.length(), l2 = word2.length();
     5         vector<vector<int>>dp(l1+1, vector<int>(l2+1, INT_MAX));
     6         for (int i = 0; i <= l1; i++)dp[i][0] = i;
     7         for (int i = 0; i <= l2; i++)dp[0][i] = i;
     8         for (int i = 1; i <= l1; i++) {
     9             for (int j = 1; j <= l2; j++) {
    10                 if (word1[i-1] == word2[j-1])
    11                     dp[i][j] =  dp[i - 1][j - 1];
    12                 else {
    13                     dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
    14                 }
    15             }
    16         }
    17         return dp[l1][l2];
    18     }
    19 };
    View Code

    貌似换成数组会更快,我还是第一次知道可以不动态申请内存这样写数组……

     1 class Solution {
     2 public:
     3     int minDistance(string word1, string word2) {
     4         int l1 = word1.length(), l2 = word2.length();
     5         int dp[l1+1][l2+1];
     6         for (int i = 0; i <= l1; i++)dp[i][0] = i;
     7         for (int i = 0; i <= l2; i++)dp[0][i] = i;
     8         for (int i = 1; i <= l1; i++) {
     9             for (int j = 1; j <= l2; j++) {
    10                 if (word1[i-1] == word2[j-1])
    11                     dp[i][j] =  dp[i - 1][j - 1];
    12                 else {
    13                     dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
    14                 }
    15             }
    16         }
    17         return dp[l1][l2];
    18     }
    19 };
    View Code
  • 相关阅读:
    Backbone.js 1.0.0源码架构分析(一)
    汤姆大叔的博客(深入理解JavaScript系列(2):揭秘命名函数表达式)
    求职之路(拿到百度、美团、趋势科技、华为offer)
    请大家注意这个网站www.haogongju.net
    后缀数组求最长重复子串
    2015阿里在线笔试题求两个字符串的最长子串
    剑指offer42:翻转单词顺序 VS 左旋转字符串(更高效、简便的解法)
    求和为s的连续正数序列
    数组中的逆序对
    求一串字符串的全排列和所有组合
  • 原文地址:https://www.cnblogs.com/yalphait/p/10371591.html
Copyright © 2011-2022 走看看