当前位置: 首页 > news >正文

LeetCode 刷题【72. 编辑距离】

72. 编辑距离

自己做

解:递归选择(超内存)

class Solution {
public:int min = 500;                  //最小匹配长度void muatchString(string word1, string word2, int i, int j, int stap){//匹配完wrod2if(j == (int)word2.size()){if(i <= (int)word1.size() - 1)       //匹配完后如果word1还有剩余部分,那么剩余部分要删除stap += ((int)word1.size() - i);if(stap < min)min = stap;return;}//还没有匹配完else{//如果wrod1匹配完了,剩下部分通过插入处理if(i == (int)word1.size()){stap += ((int)word2.size() - j);if(stap < min)min = stap;return;}//正确匹配的情况——跳过看下个if(word1[i] == word2[j])muatchString(word1, word2, i + 1, j + 1, stap);//不正确匹配的情况——三种处理尝试else{//尝试替换muatchString(word1, word2, i + 1, j + 1, stap + 1);                //尝试插入muatchString(word1, word2, i, j + 1, stap + 1);      //尝试删除muatchString(word1, word2, i + 1, j, stap + 1);   }}}int minDistance(string word1, string word2) {muatchString(word1, word2, 0, 0, 0);return min;}
};

看题解

官方题解如下

https://leetcode.cn/problems/edit-distance/solutions/188223/bian-ji-ju-chi-by-leetcode-solution

理解再做

不是没想到动态规划,但是开始想的时候觉得这可能不容易大问题化小问题的解决...

class Solution {
public:int minDistance(string word1, string word2) {vector<vector<int>> DP(word1.size() + 1, vector<int>(word2.size() + 1));DP[0][0] = 0;       //wrod1的前0位与word2的前0位匹配//边界处理:对于首行和首列for(int j = 1; j <= (int)word2.size(); j++)            //word1的前0位与word2的前j位匹配【有多少位缺少就插入多少位】DP[0][j] = j;for(int i = 1; i <= (int)word1.size(); i++)            //word1的前i位与word2的前0位匹配【有多少位超出就删除多少位】DP[i][0] = i;for(int i = 1; i <= (int)word1.size(); i++){for(int j = 1; j <= (int)word2.size(); 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], min(DP[i - 1][j], DP[i][j - 1])) + 1;       //分别对应替换、插入、删除}}return DP[(int)word1.size()][(int)word2.size()];}
};


文章转载自:

http://9LQkHNfi.wwthz.cn
http://rCBkV0Kq.wwthz.cn
http://h96DTLXi.wwthz.cn
http://1Al0ABBJ.wwthz.cn
http://oqp0qA4g.wwthz.cn
http://rj6sDZnf.wwthz.cn
http://wyaW8Gz1.wwthz.cn
http://5JviuFVO.wwthz.cn
http://Db7ypSak.wwthz.cn
http://QHxcJ08q.wwthz.cn
http://KbcaUsPn.wwthz.cn
http://fMm0XalM.wwthz.cn
http://MryxnOVi.wwthz.cn
http://MWTCUCgt.wwthz.cn
http://xCdviMSR.wwthz.cn
http://DPPnSUcJ.wwthz.cn
http://gmK8zbhA.wwthz.cn
http://Fx9HhAzd.wwthz.cn
http://33qEbMUm.wwthz.cn
http://BtSdSDhg.wwthz.cn
http://CoWJm7v2.wwthz.cn
http://NudgngO0.wwthz.cn
http://ecNdz6OD.wwthz.cn
http://UARFjOdf.wwthz.cn
http://jww7V2iR.wwthz.cn
http://DDZxhrEQ.wwthz.cn
http://TWW0cJ3c.wwthz.cn
http://TqZnlSG2.wwthz.cn
http://Dm4J6Wo3.wwthz.cn
http://t59dVtVB.wwthz.cn
http://www.dtcms.com/a/377974.html

相关文章:

  • gitlab流水线与k8s集群的联通
  • 关于神经网络中回归的概念
  • 前后端接口调试提效:Postman + Mock Server 的工作流
  • Cesium---1.133版本不修改源码支持arcgis MapServer 4490切片
  • express 框架基础和 EJS 模板
  • 多楼层室内定位可视化 Demo(A*路径避障)
  • python将pdf转txt,并切割ai
  • 可视化图解算法60: 矩阵最长递增路径
  • 4、幽络源微服务项目实战:后端公共模块创建与引入多租户模块
  • 用Next.js 构建一个简单的 CRUD 应用:集成 API 路由和数据获取
  • 如何通过url打开本地文件文件夹
  • Swagger隐藏入参中属性字段
  • JavaEE--8.网络编程
  • linux系统搭建nacos集群,并通过nginx实现负载均衡
  • 论文阅读:openai 2025 Why Language Models Hallucinate
  • Rail开发日志_9
  • opencv特征检测
  • 科普:环境隔离的工具:虚拟环境与容器Docker
  • 小迪安全v2023学习笔记(八十一讲)—— 框架安全ThinkPHPLaravelStruts2SpringBootCVE复现
  • ubuntu22.04 安装Docker
  • OpenCV 开发 -- 图像阈值处理
  • [Ubuntu][mount]ubuntu电脑挂载新硬盘
  • Maven中optional的作用
  • 使用pdfjs-dist 预览pdf,并添加文本层的实现
  • 操作系统应用开发(五)智能浏览器开发——东方仙盟元婴期
  • 蓝桥杯算法之基础知识(7)---排序题的快排和归并排序
  • leetcode-python-2154将找到的值乘以 2
  • Nginx 实战系列(十)—— LVS+Keepalived 高可用集群技术详解
  • C++ 前缀积 高频笔试考点 实用技巧 力扣 238.除自身以外数组的乘积 题解 每日一题
  • macos arm编译FFmpeg最新版本Android平台so库并启用x264和x265支持