Edit Distance Leetcode 72

S32K1XX调试--快速定位HardFault 1、背景 程序运行,发现程序跑飞到HardFault,但不清楚为什么会跑到HardFault中断处理函数去。 2、分析 要想知道为什么会跑到HardFault_Handler中去,就很有必要了解一下,程序上一条执行的是那条命令。 根据汇编指令可以定位上条指令,但如果对汇编指令不是很了解的话,那就很头疼了 根据单步运行也能实现定位,但如果程序量很大,又很杂,可能花费的时间就比较久些。 3、问题查找 提供一种不需要懂汇编指令的查找方法,方法如下: 在中断处理函数中插入以下代码,以及另外定义 阅读详情

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

给定两个字符串word1,word2,要求把word1转换成word2的最小编辑距离(这里距离指编辑的次数,修改,插入,删除都各算一步)

用动态规划来解,定义一个二维向量d[i][j]保存word1中的前i个字符转换成word2的前j个字符所需的最小编辑次数(d[0][3]表示word 1是0个字符,转换成word2前3个字符,显然是3;d[4][2]表示word1前4个字符转换成word2前2个字符所需的编辑次数…)

当word1的第i个字符(word1[i-1])与word2的第j个字符(word2[j-1])相等时,此时d[i][j]=d[i-1][j-1]

如果word1[i-1] != word2[j-1]

  1 通过替换操作把word[i-1]替换成word[j-1],那么

    d[i][j] = d[i-1][j-1] + 1;

  2 通过插入操作在word1后面插入word2[j-1], 那么就相当于计算

    d[i][j] = d[i][j-1] + 1;

  3 通过插入操作在word2后面插入word1[i-1],那么就是

    dis[i][j] = d[i-1][j] + 1;  

这种类型的题目好像只能用动态规划,不知道还能用什么方法

class Solution {
public:
    int minDistance(string word1, string word2) {
        int len1=word1.size();
        int len2=word2.size();
        vector<vector<int>> d(len1+1,vector<int>(len2+1));//二维向量d要记录word1,word2的所有字符修改的次数,最后一个下标为d[len1][len2],所以向量的长度要加1(没加的话,会出现runtime error)
        d[0][0]=0;
        for(int i=1;i<=len1;i++)d[i][0]=i;
        for(int j=1;j<=len2;j++)d[0][j]=j;

        for(int i=1;i<=len1;i++){
            for(int j=1;j<=len2;j++){
                if(word1[i-1]==word2[j-1])
                    d[i][j]=d[i-1][j-1];
                else
                    d[i][j]=min(d[i-1][j-1],min(d[i-1][j],d[i][j-1]))+1;
            }
        }   
        return d[len1][len2];    
    }
};
MATLAB 开发项目实例 1000 例专栏之第37例:基于Simulink的无人机飞行控制系统设计 通过上述项目实例,我们成功地使用Simulink设计和仿真了一个无人机的飞行控制系统。这个项目展示了从无人机建模、路径规划、控制器设计到仿真验证的完整流程。Simulink的可视化和模块化特性使得系统的建模和仿真更加直观和高效。 阅读详情

相关推荐

思科9117AP从零配置EWC模式实战:Wi-Fi6网络搭建避坑指南

本文详细介绍了思科9117AP在Wi-Fi6网络环境下配置EWC模式的实战指南,涵盖镜像准备、网络环境配置、模式转换步骤及高级运维技巧。通过EWC模式,企业可实现控制器与AP的一体化部署,显著降低成本和简化运维,特别适合中小企业Wi-Fi6网络搭建。

weixin_28318011的博客 342

算法第九周Edit Distance[hard]

Edit Distance[hard]DescriptionGiven 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 operat

south_l‘s world 305

Qwen2.5-VL模型微调实战:从零到部署的完整流程(附避坑指南)

本文详细介绍了Qwen2.5-VL视觉语言模型从零开始的完整微调与部署流程。内容涵盖使用LLaMA-Factory进行LoRA微调的关键参数配置与避坑指南,以及如何将训练好的模型权重合并,并最终利用vLLM高性能推理引擎进行部署,为开发者提供了一套可落地的实战方案。

oo7890的博客 598

LeetCode: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

Keep Moving的专栏 1342

2024高校网络安全管理运维赛题目--复现+题目+wp

官方writeup集思广益比较好的备份。

书山有路勤为径,学海无涯苦作舟 1329

【新手必看】蓝牙的HOGP和HID、GATT有什么区别和联系

让新手宏观了解蓝牙HOGP、HID、GATT这三个协议的区别和联系

qq_51976334的博客 1576

LeetCode算法题目: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

WJK的前进之路! 369

STM32G0/G4无刷电机硬件设计与FOC控制实现

无刷直流电机(BLDC)和永磁同步电机(PMSM)的矢量控制依赖于高精度电流采样、实时SVPWM调制与闭环PI调节等核心技术。其底层实现需兼顾电机本体建模、坐标变换(Clark/Park)、死区补偿及硬件资源约束。STM32G0系列凭借CORDIC协处理器与超低功耗特性,适用于成本敏感型有感FOC应用;而STM32G4集成运放、FMAC滤波器与高级定时器,可支撑高性能无感控制与多源传感融合。该技术广泛应用于机器人关节、智能云台、伺服驱动等场景,是嵌入式电机控制工程师必须掌握的工程化落地能力。

weixin_35751412的博客 129

[leetcode72] Edit Distance 解题报告

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 a chara...

初沏的茶的博客 357

由于找不到mfc110u.dll,无法继续执行代码,重新安装程序可能会解决此问题的 解决方案

本文主要介绍了由于找不到mfc110.dll,无法继续执行代码的解决方案,希望能对使用Windows的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 5万+

药物相关 PK(药代动力) 、PD(药效)指标知识

PK代表药物的药代动力学,指的是药物在体内的吸收、分布、代谢和排泄等过程,包括药物的药代动力学参数如Cmax(最大血药浓度)、Tmax(最大血药浓度出现的时间)、t1/2(半衰期)、AUC(药物曲线下面积)等。PD代表药物的药效学,指的是药物与生物体相互作用所产生的药理效应,包括药物的药效学参数如EC50(半数最大有效浓度)、ED50(半数最大效应剂量)、Emax(最大效应)等。PD(Pharmacodynamics)则研究药物的药效学特性,如药物的作用机制、效力、最大疗效、最小毒性等。

weixin_42357472的博客 2万+

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 word:a) In

且听疯吟的博客 400

R3LIVE项目实战(3) —双目相机与激光雷达 lidar_camera_calib联合标定

lidar_camera_calib是一个在无目标环境中,用于高分辨率LiDAR(例如Livox)和摄像机之间准确的外部标定工具。我们的算法可以在室内和室外场景中运行,并且只需要场景中的边缘信息。如果场景适合,我们可以实现类似于或甚至超过基于目标的方法的像素级准确度。我们使用标定的外部参数给点云上色,并与实际图像进行比较。A和C是点云的局部放大视图。B和D是与A和C中的点云对应的摄像机图像的部分。

几度春风里的博客 4068

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 5681

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

jre9下载 jdk9

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

LeetCode 72. Edit Distance 题解

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

cannonjinx的博客 4237

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【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 3112

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode72——Edit Distance

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

上善若水 1579

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的博客 333

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的博客 226

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

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

916

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...

卷卷萌的博客 297

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的博客 333

【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

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
上一篇: Simplify Path Leetcode71
下一篇: Multiply Strings leetcode 43
bae_
博客等级 码龄12年 0粉丝 25原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值