注意min函数只能比较两个参数
class Solution { public: int minDistance(string word1, string word2) { int length1 = word1.length(); int length2 = word2.length(); vector<vector<int>> result(length1+1,vector<int>(length2+1)); for(int i = 0;i <= length1;i++) result[i][0] = i; for(int j = 0;j <= length2;j++) result[0][j] = j; for(int i = 1;i <= length1;i++){ for(int j = 1;j <= length2;j++){ if(word1[i-1] == word2[j-1]) result[i][j] = result[i-1][j-1]; else result[i][j] = min(result[i-1][j-1] + 1,min(result[i-1][j] + 1,result[i][j-1] + 1)); } } return result[length1][length2]; } };