LeetCode | 72. Edit Distance

【冷冻电镜】RELION5.0使用教程总结 对于您自己的数据,您可能希望使用各种倾斜系列对齐方法,然后比较每种方法生成的断层扫描图像的质量(参见下一步)。通过检查 CtfFind job 的 tilt_series 目录中的star文件来检查几个倾斜系列的散焦值。GitHub上也有人反应这个问题,目前我的解决方案是下载了较低版本的CTFFIND的,降级到4.1.10可以成功运行。此外,logfile.pdf 文件包含所有显微照片的有用参数图,例如散焦、像散、估计分辨率等,以及整个数据集中这些值的直方图。的目录,以及指向此目录的符号链接。 阅读详情

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

思路

对于字符串 str1 和 str2,dp[i][j]为考虑str1的0~i, str2的0~j的最短编辑距离。

  • 若str1[i] == str2[j]:dp[i][j] = dp[i-1][j-1]
  • 若str1[i] != str2[j],有三种操作,让两个字符串对齐:
    • 1、替换掉这个字符,使得str1[i] == str2[j],dp[i][j] = dp[i-1][j-1]+1
    • 2、删去str1中的这个字符,使得问题变为考虑str1[0~i-1]和str2[0~j]对齐问题,dp[i][j] = dp[i-1][j]+1
    • 3、删去str2[j],dp[i][j] = dp[i][j-1]+1
      综合上面三种情况,找出最小值作为dp[i][j]的值

难点在于边界情况的处理!

代码

代码1:静态数组,内存开销大,86 ms

class Solution {
public:
    int mymin(int a,int b,int c)
    {
        return min(min(a,b),c);
    }

    int minDistance(string word1, string word2)
    {
        int len1 = word1.length(), len2 = word2.length();
        int dp[500][500] = {};
        int flag1 = 1, flag2 = 1;

        if(len1 == 0)
            return len2;
        if(len2 == 0)
            return len1;

        if(word1[0]==word2[0])
        {
            dp[0][0] = 0;
            flag1 = flag2 = 0;
        }
        else
            dp[0][0] = 1;

        //1-1:dinitrophenylhydrazine
        //1-2:phenylhydrazine
        //2-1:sea
        //2-2:eat

        for(int i=1;i<len1;i++)
        {
            /*解释下面这个 if
             此后只要删除无关字符即可。例如:abcd  caret
             dp[0][1]=1;
             dp[0][2]=2,而不是3
             */
            if(word1[i] == word2[0])
            {
                dp[i][0] = i;
                flag1 = 0;
            }
            else
                dp[i][0] = i+flag1;
        }
        for(int j=1;j<len2;j++)
        {
            if(word1[0] == word2[j])
            {
                dp[0][j] = j;
                flag2 = 0;
            }
            else
                dp[0][j] = j+flag2;
        }

        for(int i=1;i<len1;i++)
        {
            for(int j=1;j<len2;j++)
            {
                if(word1[i] == word2[j])
                    dp[i][j] = dp[i-1][j-1];
                else
                {
                    dp[i][j] = mymin(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1;
                }
            }
        }

        return dp[len1-1][len2-1];
    }
};

代码2:动态数组法,节省内存空间,13 ms

class Solution {
public:
    int mymin(int a,int b,int c)
    {
        return min(min(a,b),c);
    }

    int minDistance(string word1, string word2)
    {
        int len1 = word1.length(), len2 = word2.length();
        vector<vector<int> > dp;
        int flag1 = 1, flag2 = 1;

        if(len1 == 0)
            return len2;
        if(len2 == 0)
            return len1;

        vector<int> tmp;
        tmp.push_back(0);
        dp.push_back(tmp);
        if(word1[0]==word2[0])
        {
            dp[0][0] = 0;
            flag1 = flag2 = 0;
        }
        else
            dp[0][0] = 1;

        //1-1:dinitrophenylhydrazine
        //1-2:phenylhydrazine
        //2-1:sea
        //2-2:eat

        for(int i=1;i<len1;i++)
        {
            /*解释下面这个 if
             此后只要删除无关字符即可。例如:abcd  caret
             dp[0][1]=1;
             dp[0][2]=2,而不是3
             */
            vector<int> t;
            if(word1[i] == word2[0])
            {
                t.push_back(i);
                flag1 = 0;
            }
            else
                t.push_back(i+flag1);
            dp.push_back(t);
        }
        for(int j=1;j<len2;j++)
        {
            if(word1[0] == word2[j])
            {
                dp[0].push_back(j);
                flag2 = 0;
            }
            else
                dp[0].push_back(j+flag2);
        }

        for(int i=1;i<len1;i++)
        {
            for(int j=1;j<len2;j++)
            {
                if(word1[i] == word2[j])
                    dp[i].push_back(dp[i-1][j-1]);
                else
                {
                    dp[i].push_back(mymin(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1);
                }
            }
        }

        return dp[len1-1][len2-1];
    }
};

代码3:对边界的处理更熟练,13 ms

class Solution { 
public:
    int minDistance(string word1, string word2) { 
        int m = word1.length(), n = word2.length();
        vector<vector<int> > dp(m + 1, vector<int> (n + 1, 0));
        for (int i = 1; i <= m; i++)
            dp[i][0] = i;
        for (int j = 1; 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(dp[i - 1][j - 1] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j] + 1));
            }
        }
        return dp[m][n];
    }
};
SPI为什么不需要加上拉电阻 大家好,我是记得诚。I2C因为其开漏的结构,无法输出高电平,所以外部加上拉电阻。我们在使用SPI的时候,发现是不需要加上拉电阻的。主要因为SPI多为推挽的结构,推挽电路,高低电平都可以输出,所以外部不需要加上拉电阻。推挽电路可以用2个三极管或者2个MOS管搭建而成。 阅读详情

相关推荐

OpenCV+ArUco实战:如何用Python实现高精度二维码测距(附完整代码)

本文详细介绍了如何使用Python和OpenCV实现基于ArUco标签的高精度单目视觉测距。通过相机标定、标签检测和三维位置估算,结合完整代码示例,帮助开发者在机器人导航、工业自动化等场景中实现厘米级距离测量,并探讨了精度优化与三维可视化等进阶应用。

weixin_29052717的博客 478

LeetCode(72)Edit Distance

题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a wor

BUILD 5675

三极管特性曲线看不懂?手把手教你用Multisim仿真分析放大电路频率响应

本文针对模拟电路初学者,详细讲解如何利用Multisim软件进行电路仿真,将抽象的三极管特性曲线与放大电路频率响应分析变得直观可视。通过手把手教学,指导读者从导入三极管SPICE模型、仿真输出特性曲线,到分析动态工作点,最终使用波特图仪深入分析频率响应并进行补偿设计,有效帮助理解电路原理并指导实践调试。

weixin_29230059的博客 125

LeetCode 72. Edit Distance 题解

本题是动态规划的经典难题,也是字符串算法的核心问题之一。状态定义:dp[i][j] 表示转换子串的最小操作数状态转移:考虑插入、删除、替换三种操作边界条件:空字符串的转换通过本题可以深入理解二维动态规划的状态设计和转移方程的建立。

cannonjinx的博客 4234

利用Python爬虫获取淘宝商品评论:实战案例分析

在数字化时代,数据的价值日益凸显,尤其是对于电商平台而言,商品评论作为用户反馈的重要载体,蕴含着丰富的信息。本文将详细介绍如何利用Python爬虫技术获取淘宝商品评论,包括代码示例和关键步骤解析。

2401_87849335的博客 2835

java modbus通讯协议_Modbus通 讯 协 议

电磁流量计Modbus通 讯 协 议(版本号:9000-K V9.1.1)2020年01月01日通讯协议针对KFD电磁流量计工业应用设计,版本:9000-K V9.1.1,该版本主要用于实时数据采集、流量测量、流量累计控制及部分参数的修改。一、主机系统通讯部件要求1.国际标准RS-485通讯接口部件,不小于11Bytes 的通信缓冲区(FIFO),支持1200、2400、4800、96...

weixin_39956612的博客 1012

【LeetCode】72. Edit Distance

微信公众号 72.Edit Distance Description: Given two wordsword1andword2, find the minimum number of operations required to convertword1toword2. You have the following 3 operations permitted on a ...

Microstrong 3111

6、 实时家禽健康识别与疾病检测系统的深入解析

本文深入解析了一种基于物联网的实时家禽健康识别与疾病检测系统,结合图像分析和音频分析技术,能够有效识别病鸡并防止疾病传播。文章详细介绍了系统的测试环境、数据采集与预处理方法、图像与音频分析流程,并对系统性能进行了全面评估。最后提出了未来改进方向,包括多模态融合、深度学习应用及实时性优化等。

weixin_28840811的博客 187

LeetCode72——Edit Distance

十一月太忙,好一阵没写博客没刷题,现在事情忙完了,生活规律要逐步走入正轨。 -------------------------------------------------------------------------------------------------- LeetCode72——Edit Distance 动态规划一直不是我的强项,但是这道题恰好不知道在哪里见到过。 首先

上善若水 1579

2.4 提示词优化

我们在使用大模型的时候,同一任务使用不同表述的提示词,可能得到差异巨大的结果,因为模型的性能高度依赖输入提示词的质量。我们可以通过提示词优化来更高效地获取理想输出。

从0到1,突破自己 1010

麻雀搜索算法(SSA)详解:从鸟群智能到优化实践

麻雀搜索算法(SSA)是一种新型群智能优化算法,模拟麻雀群体的觅食行为和反捕食机制。该算法将优化问题转化为麻雀群体对食物源的搜索过程,通过发现者、加入者和警戒者的角色分工实现高效优化。SSA具有原理新颖、参数合理、收敛快速等特点,在函数优化、工程设计和机器学习等领域展现出良好应用前景。算法通过数学模型模拟麻雀的觅食策略,包括发现者全局探索、加入者局部开发和警戒者危险预警机制,有效平衡了全局搜索与局部开发能力。SSA相比传统优化算法具有更好的收敛性和优化效果。

qq_42212808的博客 1075

LeetCode 72. Edit Distance

[Hard] LeetCode 72. Edit Distance 链接: https://leetcode.com/problems/edit-distance/ 题目描述: Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: I

MrJustin的博客 332

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

单词拼写纠正-03-72.力扣编辑距离 leetcode edit-distance

如果问题变得更复杂,例如目标字符串是一个大字典中的单词,并且需要用最少的操作将一个单词转化为目标字典中的某个单词时,可以使用 A* 搜索算法。但是,由于动态规划中的每个状态只依赖于上一行和当前行的数据,因此可以将空间优化为一维数组,从而减少空间复杂度。不过,A* 在本问题中的应用较少,因为它需要定义一个启发式函数,并且在大多数情况下,动态规划已经足够高效。记忆化递归是暴力递归的一种优化,通过在递归过程中记录已经计算过的子问题的结果,避免了重复计算。来记录子问题的结果,动态规划能够从底向上计算出最小操作数。

916

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

leetcode72 edit distance

题目: Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a char...

sinat_37517996的博客 224

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode72:Edit Distance

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a chara...

卷卷萌的博客 296

leetcode 72. Edit Distance

一 题目 Given two wordsword1andword2, find the minimum number of operations required to convertword1toword2. You have the following 3 operations permitted on a word: Insert a character Delet...

bohu83的博客 332

【Leetcode-72】Edit Distance

对于(i-1, j)时,d(i-1, j)表示完成从A[0, i-1]到B[0, j]的编辑次数,即现在A[0,i-1]=B[0,j],对于(i,j),我们直接把A[i]删除即可完成编辑,因此(i-1,j)对应于把A[i]删除的一次操作 对于(i, j-1)时,d(i, j-1)表示完成从A[0, i]到B[0, j-1]的编辑次数,即现在A[0,i]=B[0,j-1],对于(i,j),我们直接...

victorzzzz的专栏 228

leetcode 72. Edit Distance 编辑距离

题目要求 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1: 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 'h' 替换为 'r') rorse -> rose...

爱喝舒化奶的博客 552

LeetCode72 Edit Distance

LeetCode72 Edit Distance问题描述 Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3

yanqinghe123的专栏 264

【Leetcode】 72. Edit Distance

函数中,使用大括号括起来的是可迭代对象,如列表、元组、集合等。如果我们要对一个可迭代对象中的元素进行比较,那么就可以使用。函数中,使用大括号括起来的是可迭代对象,如列表、元组、集合等。如果我们要对一个可迭代对象中的元素进行比较,那么就可以使用。表示按照子列表元素之和的大小进行比较,返回元素之和最大的子列表。表示按照子列表元素之和的大小进行比较,返回元素之和最大的子列表。如果要从这个列表中找到元素之和最大的子列表,我们可以使用。如果要从这个列表中找到元素之和最大的子列表,我们可以使用。

小白兔奶糖の博客 333

Java解决LeetCode72题 Edit Distance

题目描述 地址 : leetcode.com/problems/ed… 题目描述 思路 使用dp[i][j]用来表示word1的0~i-1、word2的0~j-1的最小编辑距离 我们可以知道边界情况:dp[i][0] = i、dp[0][j] = j,代表从 "" 变为 dp[0~i-1] 或 dp[0][0~j-1] 所需要的次数 同时对于两个字符串的子串,都能分为最后一个字符相等或者不等...

weixin_34249678的博客 280

LeetCode:72. Edit Distance

LeetCode:72. Edit Distance Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert ...

Wisimer 263

LeetCode 72 Edit Distance

我写了两种方案的。一种是用的递归方式,结果超时了。一种是直接用数组模拟删、替换、插入操作。删操作对应的是DP[i][j-1],替换操作对应的是DP[i-1][j-1],插入操作对应额的是DP[i-1][j].此外要注意初始化的情形,如果word1是0,那么对应的最小步骤是word2的长度;如果word2是0,那么对应的最小步骤是word1的长度。OK。 解法一:超时的那种。。。。 class

input1991的专栏 354
上一篇: LeetCode | 71. Simplify Path
下一篇: Boot Camp分区时强制终止导致硬盘空间消失的解决方法
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值