class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param str1 string字符串 * @param str2 string字符串 * @return int整型 */ int editDistance(string str1, string str2) { // write code here int n = str1.length(), m = str2.length(); vector<int> prev(m + 1), cur(m + 1); for (int j = 0; j <= m; j++) prev[j] = j; for (int i = 1; i <= n; i++) { cur[0] = i; for (int j = 1; j <= m ; j++) { int cost = str1[i - 1] == str2[j - 1] ? 0 : 1; cur[j] = min({prev[j - 1] + cost, prev[j] + 1, cur[j -1] + 1}); } swap(prev, cur); } return prev[m]; } };