标签:min int == end using pre ace 题目 基本
一:解题思路
这道题目和leetcode72题,基本上是一样的,可以放在一起进行学习。
二:完整代码示例 (C++版和Java版)
C++:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int min3(int a, int b, int c) { return min(a,min(b,c)); } int minDistance(string word1, string word2) { if (word1.size() == 0 && word2.size() == 0) return 0; int m = word1.size() + 1; int n = word2.size() + 1; vector<vector<int>> d(m,vector<int>(n,0)); for (int j = 0; j < n; j++) d[0][j] = j; for (int i = 0; i < m; i++) d[i][0] = i; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (word1[i - 1] == word2[j - 1]) d[i][j] = d[i-1][j-1]; else { d[i][j] = min3(d[i-1][j-1],d[i-1][j],d[i][j-1])+1; } } } return d[m-1][n-1]; } int main() { string s1 = ""; string s2 = ""; while (cin >> s1 >> s2) { cout << minDistance(s1,s2) << endl; } return 0; }
标签:min int == end using pre ace 题目 基本
原文地址:https://www.cnblogs.com/repinkply/p/13432494.html