Leetcode 72. Edit Distance

Spring Boot 3.2 集成 Shiro 2.0.1 踩坑实录:从 javax.servlet 到 jakarta.servlet 的完整迁移指南 本文详细介绍了Spring Boot 3.2集成Shiro 2.0.1时遇到的从javax.servlet到jakarta.servlet的迁移问题,提供了完整的解决方案和迁移指南。通过Maven依赖配置和代码调整,帮助开发者顺利跨越Jakarta EE的兼容性鸿沟,确保Shiro权限控制在Spring Boot 3.2环境中正常运行。 阅读详情

Leetcode 72. Edit Distance

source url

题目描述

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
输出:需要修改、删除、插入的次数

思路

动态规划:用L(i,j)表示word1[0:i]与word2[0:j]的编辑距离

for i = 0,1, 2, ..., m:
    L(i,0) = i
for j = 0,1, 2, ..., n:
    L(0,j) = j

for i = 0,1, 2, ..., m:
    for j = 0,1, 2, ..., n:
        L(i,j) = min(L(i-1,j)+1, L(i,j-1)+1, diff+L(i-1,j-1))

return L(m,n)

代码

class Solution {
public:
    int minDistance(string word1, string word2) {
        if(word2.length()==0||word1.length()==0) return max(word2.length(),word1.length());

        int L[word1.length()+1][word2.length()+1];

        for(int i =0;i<=word1.length();i++)
            L[i][0] = i;
        for(int j =0;j<=word2.length();j++)
            L[0][j] = j;

        for(int i = 1;i<=word1.length();i++){
            for(int j =1;j<=word2.length();j++){

                if(j>=1&&i>=1){
                    int diff = (word1[i-1]!=word2[j-1]);
                    L[i][j] = min(min(1+L[i-1][j],1+L[i][j-1]),diff+L[i-1][j-1]);
                }
            }
        }
        return L[word1.length()][word2.length()];


    }
};
Yocto系列讲解[入门篇] 1 - 快速入门熟悉Yocto的构建 yocto quick start up带大家快速上手 阅读详情

相关推荐

科研软件|Adobe Photoshop 2024 (PS 2024)

科研软件|Adobe Photoshop 2024 (PS 2024)

m0_55499682的博客 5518

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

初沏的茶的博客 356

从零开始:如何用PSINS工具箱构建你的第一个IMU误差模型

本文详细介绍了如何使用严恭敏教授开发的PSINS工具箱构建IMU误差模型,涵盖从基础参数设置到高级误差建模的全过程。通过imuerrset函数配置陀螺零偏、随机游走等核心参数,并结合实战案例展示消费级MEMS器件的误差建模方法,帮助用户提升惯性导航系统的精度与可靠性。

weixin_29284885的博客 323

Leetcode全字符问题

目录 1、编号3 Longest Substring Without Repeating Characters 2、编号6 ZigZag Conversion 3、编号20 Valid Parentheses 4、编号28 Implement strStr() 5、编号31 Longest Valid Parentheses 6、编号42 Multiply String

無名黑洞 4267

安装IC Complier 2(安装synposys的其他工具也可以来看看,因为殊途同归)

安装ICC2

hjt66666的博客 5771

Unity地形数据高效转换:从Terrain到3DMAX的OBJ导出实战

本文详细解析了Unity地形数据无法直接导入3DMAX的原因,并提供了高效转换的实战方案。通过编写自定义编辑器脚本,将Unity Terrain的高度图数据“烘焙”为静态网格模型,并以OBJ格式导出。文章重点讲解了核心代码逻辑、分辨率选择对模型精度的影响,以及在3DMAX中导入和优化模型的实用技巧,旨在打通Unity与3DMAX之间的数据协作壁垒。

sky77的博客 449

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

Johnsonjjj的博客 438

Mac mini M2 部署 OpenClaw:最稳 7×24 本地 AI 服务器

Mac mini M2 凭借其强大的 M2 芯片、低功耗和静音特性,成为部署 OpenClaw AI 服务器的理想平台。本文详细介绍了从系统配置到 OpenClaw 部署的全过程,包括 Docker 环境搭建、docker-compose 编排、性能优化方案以及开机自启设置。通过统一内存架构和高效 SSD,M2 版 Mac mini 在 AI 推理速度上显著优于前代产品,同时保持极低的功耗(仅 8W),适合 7×24 小时稳定运行。部署方案充分利用了容器化技术,确保了服务的可靠性和可维护性。

一键难忘的博客 2955

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

计算数列位置N的值

本文介绍了一个数列生成问题,要求根据输入的两个整数M和N生成特定规则的数列,并输出第N个位置上的数值。数列生成规则为:前M个元素为1到M;后续元素根据前M个元素是否重复来决定计算方式(求和或求差)。文章提供了C++、Java和Python三种语言的实现方案,均包含输入处理、数列初始化、循环计算和结果输出等步骤。各语言实现中均使用集合去重特性判断重复元素,并通过内置函数或自定义方法求取最大值和最小值。该问题主要考察基础编程能力,包括数组/列表操作、最值计算和重复元素判断等基本编程技巧。

面试高手的博客 663

如何对正射影像DOM(卫星影像、无人机航拍影像)进行拼接、合并以及去黑边(附 Global Mapper Pro 24.0 安装包(中文注册版))

为什么要对正射影像DOM进行拼接?影像去黑边、影像拼接的操作和步骤。

从地图看世界的专栏 4300

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

My_Jobs的专栏 937

一文吃透 SAP HANA JSON Document Store:从架构到实战的完整指南

本文详细介绍了SAP HANA的JSON Document Store功能,包括其定位、架构、典型场景以及具体使用方法。文章首先分析了半结构化数据场景下传统关系模型的局限性,说明Document Store如何通过集合(Collection)存储JSON文档来提供灵活性。随后详细阐述了启用方式、SQL语法、与Column Store的联动机制,并提供了大量可直接复用的实战案例。特别强调了Document Store作为HANA内建服务的优势,包括企业级一致性、备份恢复等能力。最后给出了数据类型处理和跨引擎协

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 202

LeetCode 72. Edit Distance 题解

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

cannonjinx的博客 4234

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

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

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

单词拼写纠正-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...

卷卷萌的博客 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
上一篇: Leetcode 300. Longest Increasing Subsequence
下一篇: Leetcode 91. Decode Ways
vincentCheungM
博客等级 码龄10年 4粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值