Skip to content

Commit 2c3cbd9

Browse files
authored
Merge pull request #70 from lixiaoyu0123/master
leetcode46
2 parents 5b8c357 + 00bcb19 commit 2c3cbd9

22 files changed

+419
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Output: [3,5]
263263
> 1. The order of the result is not important. So in the above example, [5, 3] is also correct.
264264
> 2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
265265
266-
46. ***【Edit Distance】*** Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
266+
46. ***[【Edit Distance】](./doc/Leetcode46/Leetcode46.md)*** Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
267267
You have the following 3 operations permitted on a word:
268268
1. Insert a character
269269
2. Delete a character

code/Leetcode46/edit_dist_dp.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include "edit_dist_dp.h"
4+
5+
int editDistDp(const std::string &word1, const std::string &word2) {
6+
int m = word1.length();
7+
int n = word2.length();
8+
9+
// 有一个字符串为空串
10+
if (m * n == 0){
11+
return m + n;
12+
}
13+
14+
// DP 数组
15+
std::vector<std::vector<int> > dp((m + 1),std::vector<int>(n + 1));
16+
17+
// 边界状态初始化
18+
for (int i = 0; i < m + 1; i++) {
19+
dp[i][0] = i;
20+
}
21+
for (int j = 0; j < n + 1; j++) {
22+
dp[0][j] = j;
23+
}
24+
25+
// 计算所有 DP 值
26+
for (int i = 1; i <= m; i++) {
27+
for (int j = 1; j <= n; j++) {
28+
if (word1[i - 1] == word2[j - 1]) {
29+
dp[i][j] = dp[i - 1][j - 1];
30+
}else {
31+
dp[i][j] = 1 + std::min(dp[i - 1][j],
32+
std::min(dp[i][j - 1],
33+
dp[i - 1][j - 1]));
34+
}
35+
}
36+
}
37+
return dp[m][n];
38+
}

code/Leetcode46/edit_dist_dp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <string>
2+
3+
int editDistDp(const std::string &word1, const std::string &word2);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include "edit_dist_dp_best.h"
4+
5+
int editDistDpBest(const std::string &word1, const std::string &word2){
6+
int m = word1.length();
7+
int n = word2.length();
8+
9+
if (m * n == 0) {
10+
return m + n;
11+
}
12+
13+
std::vector<std::vector<int> > dp((2), std::vector<int>(n + 1));
14+
15+
int iSize = dp.size();
16+
for (int i = 0; i < iSize; i++) {
17+
int jSize = dp[i].size();
18+
for (int j = 0; j < jSize; j++) {
19+
dp[i][j] = 0;
20+
}
21+
}
22+
23+
//³õʼ»¯i=0µÄÒ»ÐÐÖµ
24+
for (int i = 0; i <= n; i++){
25+
dp[0][i] = i;
26+
}
27+
28+
for (int i = 1; i <= m; i++) {
29+
for (int j = 0; j <= n; j++) {
30+
if (j == 0) {
31+
dp[i % 2][j] = i;
32+
}else if (word1[i - 1] == word2[j - 1]) {
33+
dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
34+
}else {
35+
dp[i % 2][j] = 1 + std::min(dp[(i - 1) % 2][j],
36+
std::min(dp[i % 2][j - 1],
37+
dp[(i - 1) % 2][j - 1]));
38+
}
39+
}
40+
}
41+
42+
return dp[m % 2][n];
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <string>
2+
3+
int editDistDpBest(const std::string &word1, const std::string &word2);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <algorithm>
2+
#include "edit_dist_recursion.h"
3+
4+
int editDistRecursion(const std::string &word1, const std::string &word2) {
5+
return editDist(word1, word2, word1.size(), word2.size());
6+
}
7+
8+
static int min(int x, int y, int z)
9+
{
10+
return std::min(std::min(x, y), z);
11+
}
12+
13+
14+
int editDist(const std::string &word1 , const std::string &word2, int m, int n) {
15+
// If first string is empty, the only option is to
16+
// insert all characters of second string into first
17+
if (m == 0){
18+
return n;
19+
}
20+
21+
// If second string is empty, the only option is to
22+
// remove all characters of first string
23+
if (n == 0){
24+
return m;
25+
}
26+
// If last characters of two strings are same, nothing
27+
// much to do. Ignore last characters and get count for
28+
// remaining strings.
29+
if (word1[m - 1] == word2[n - 1]) {
30+
return editDist(word1, word2, m - 1, n - 1);
31+
}
32+
// If last characters are not same, consider all three
33+
// operations on last character of first string, recursively
34+
// compute minimum cost for all three operations and take
35+
// minimum of three values.
36+
return 1 + min(editDist(word1, word2, m, n - 1), // Insert
37+
editDist(word1, word2, m - 1, n), // Remove
38+
editDist(word1, word2, m - 1, n - 1) // Replace
39+
);
40+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <string>
2+
3+
int editDistRecursion(const std::string &word1 , const std::string &word2);
4+
5+
int editDist(const std::string &word1, const std::string &word2, int m, int n);

code/Leetcode46/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdlib.h>
2+
#include <string>
3+
#include <chrono>
4+
#include <iostream>
5+
#include "edit_dist_recursion.h"
6+
#include "edit_dist_dp.h"
7+
#include "edit_dist_dp_best.h"
8+
9+
int main(int argc, char* argv[]){
10+
if (argc < 4) {
11+
std::cout << "parameter count is error!\n";
12+
return -1;
13+
}
14+
15+
std::string word1(argv[1]);
16+
std::string word2(argv[2]);
17+
std::string way(argv[3]);
18+
int dist = 0;
19+
auto beginTime = std::chrono::high_resolution_clock::now();
20+
21+
if (way == "recursion") {
22+
dist = editDistRecursion(word1, word2);
23+
}else if (way == "dp") {
24+
dist = editDistDp(word1, word2);
25+
}else if (way == "dp_best") {
26+
dist = editDistDpBest(word1, word2);
27+
}else {
28+
std::cout << "parameter is err!\n";
29+
return -1;
30+
}
31+
auto endTime = std::chrono::high_resolution_clock::now();
32+
auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - beginTime);
33+
std::cout << "elapsed time is " << elapsedTime.count() << " microseconds" << std::endl;
34+
35+
std::cout<< "the min eidt distance is:" << dist << "\n";
36+
return 0;
37+
}

0 commit comments

Comments
 (0)