solution.cpp 588 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	int minDistance(string word1, string word2)
	{
		int l1 = word1.length();
		int l2 = word2.length();
		vector<int> dp(l2 + 1);
		for (int i = 0; i <= l2; i++)
		{
			dp[i] = i;
		}
		int up = 0;
		for (int i = 1; i <= l1; i++)
		{
			int left_up = dp[0];
			dp[0] = i;
			for (int j = 1; j <= l2; j++)
			{
				up = dp[j];
				if (word1[i - 1] == word2[j - 1])
				{
					dp[j] = left_up;
				}
				else
				{
					dp[j] = 1 + min(left_up, min(up, dp[j - 1]));
				}
				left_up = up;
			}
		}
		return dp[l2];
	}
};