【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) Replace a character



题解:动态规划问题。

用数组dp[i][j]表示从word1[0,i]变换到word2[0,j]所需要的最小变换。

那么dp[i][j] = word1(i) == word2(j)? dp[i-1][j-1]: min(dp[i-1,j], dp[i,j-1], dp[i-1,j-1])+1;

要特别注意的是第一行和第一列的处理问题。一种方法是把dp的大小设置为dp[word1.length+1][word2.length+1],然后dp[0][i] = dp[i][0] = i,表示从空字符串变换到长度为i的字符串相互变换至少需要i步。个人觉得这是比较好的一种方法,但是当时没有想到。

我想到的是另外 一种方法,而且在这里被坑了好久。以行为例: dp[0][i] = Math.max(dp[0][i-1] + (wordchars1[0]== wordchars2[i]?0:1),i); 表示如果word1的第0个字符和word2的第i个字符相等,那么需要dp[0][i-1]步变换。但是这里有个问题,就是当word1 = "pneumu", word2 = "up"的时候,此时从字符串"u"变换到"pneum"需要4步,在计算从"u"变换到"pneumu"的时候,如果直接用dp[0][i-1]得到需要4步,但这是不可能的,因为两个字符串的长度就相差了5,至少需要5步,所以才有了外面的max函数判断,两个字符串互相转换的最少步数不会低于两个字符串长度之差。

最终代码如下:

 1 public class Solution {
 2     public int minDistance(String word1, String word2) {
 3         int m = word1.length();
 4         int n = word2.length();
 5         if(m == 0)
 6             return n;
 7         if(n == 0)
 8             return m;
 9         int[][] dp = new int[m][n];
10
11         char[] wordchars1 = word1.toCharArray();
12         char[] wordchars2 = word2.toCharArray();
13
14         dp[0][0] = wordchars1[0] == wordchars2[0]?0:1;
15         for(int i = 1;i < n;i++)
16             dp[0][i] = Math.max(dp[0][i-1] + (wordchars1[0]== wordchars2[i]?0:1),i);
17         for(int i = 1;i < m;i++)
18                dp[i][0] = Math.max(dp[i-1][0] + (wordchars1[i]== wordchars2[0]?0:1),i);
19
20         for(int i = 1;i < m;i++){
21             for(int j = 1;j < n;j++){
22                 if(wordchars1[i] == wordchars2[j] )
23                     dp[i][j] = dp[i-1][j-1];
24                 else{
25                     dp[i][j] = 1 + Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
26                 }
27             }
28         }
29
30         return dp[m-1][n-1];
31     }
32 }

【leetcode刷题笔记】Edit Distance

时间: 2024-09-29 01:10:08

【leetcode刷题笔记】Edit Distance的相关文章

LintCode刷题笔记-- 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: Insert a character Delete a character Replace a

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort. 题解:实现链表的插入排序. 要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦.所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了. 代码如下: 1 /** 2 * Defi

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的