zoukankan      html  css  js  c++  java
  • leetcode 72. 编辑距离

    /*****
    定义状态:
    DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符
    DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数;
    状态转移:
    for i:[0,m]
        for j:[0,n]
        if(word1[i-1]==word2[j-1])
            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;
    return DP[m][n];
    
    ******/
    
    
    class Solution {
    public:
        int minDistance(string word1, string word2) {
            int m=word1.size(),n=word2.size();
            vector<vector<int> > DP(m+1,vector(n+1,0));
            //初始化
            for(int i=0;i<=m;i++){
                DP[i][0]=i;
            }
            for(int j=0;j<=n;j++){
                DP[0][j]=j;
            }
            //状态转移
            for(int i=1;i<=m;i++){
                for(int j=1;j<=n;j++){
                    if(word1[i-1]==word2[j-1])
                        DP[i][j]=DP[i-1][j-1];
                    else
                        DP[i][j]=min(min(DP[i-1][j],DP[i][j-1]),DP[i-1][j-1])+1;
                }
            }
            return DP[m][n];
        }
    };
  • 相关阅读:
    IOC
    软件问题
    POJO和JavaBean
    tail命令
    实现质数遍历并输出所需时间
    完数
    break、continue
    *各种形状
    for、while、do-while
    jenkins实现maven项目自动化部署tomcat
  • 原文地址:https://www.cnblogs.com/joelwang/p/10875673.html
Copyright © 2011-2022 走看看