problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::edit_distance::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int minDistance (string word1, string word2)
 

详细描述

在文件 leetcode.h2523 行定义.

成员函数说明

◆ minDistance()

int leetcode::edit_distance::Solution::minDistance ( string  word1,
string  word2 
)
static

在文件 leetcode.cpp6781 行定义.

6781 {
6782 // dp[i][j] is the minimum number of operations required to convert word1[0..i] to word2[0..j]
6783 vector dp(word1.length() + 1, vector<int>(word2.length() + 1));
6784 // if word[i] == word2[j], dp[i][j] = dp[i-1][j-1]; else dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
6785 for(int j = 0; j <= word2.length(); j++) {
6786 dp[0][j] = j;
6787 }
6788 for(int i = 0; i <= word1.length(); i++) {
6789 dp[i][0] = i;
6790 }
6791 for(int i = 1; i <= word1.length(); i++) {
6792 for(int j = 1; j <= word2.length(); j++) {
6793 if(word1[i - 1] == word2[j - 1]) {
6794 dp[i][j] = dp[i - 1][j - 1];
6795 } else {
6796 dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
6797 }
6798 }
6799 }
6800 return dp.back().back();
6801 }

被这些函数引用 leetcode::edit_distance::TEST().


该类的文档由以下文件生成: