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

【Hot100】LeetCode—72. 编辑距离

目录

  • 1- 思路
    • 题目识别
    • 动规五部曲
  • 2- 实现
    • 72. 编辑距离——题解思路
  • 3- ACM 实现


  • 原题链接:72. 编辑距离

1- 思路

题目识别

  • 识别1 :两个字符串之间相互转换,增、删、替换 最少的操作次数

动规五部曲

  • 1- 定义 dp 数组
    • dp[i][j] 代表,长度为 i-1nums1 和长度为 j-1nums2 的编辑距离,也就是使二者相等的最小操作次数
  • 2- 递推公式
    • 如果两个字符相同了dp[i][j] = dp[i-1][j-1],因为相同所以不需要任何操作
    • 否则
      • 删除 word1 操作:dp[i-1][j] + 1
      • 删除 word2 操作:dp[i][j-1] + 1
      • 替换操作:dp[i-1][j-1] + 1
    • 因此 dp[i][j] = Math.min(dp[i-1][j] + 1,Math.min(dp[i][j-1]+1,dp[i-1][j-1]+1);
  • 3- 初始化
    • 第一行、第一列初始化为对应的下标。
    • i1 遍历到 len1 比如 dp[i][0] 则初始化为i
  • 4- 递推
    • 由于 [i][j] 根据 [i-1][j-1]来,所以从上到下,从左到右遍历

2- 实现

72. 编辑距离——题解思路

在这里插入图片描述

    public int minDistance(String word1, String word2) {
        // 1. 定义dp数组
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];

        // 2.递推公式
        // 相等 dp[i][j] = dp[i-1][j-1];
        // 不相等 dp[i][j] = Math.min(dp[i-1]+1,dp[i-1][j-1]+1);

        // 3.初始化
        for (int i = 0; i <= word1.length(); i++) {
            dp[i][0] = i;
        }
        for (int j = 0; j <= word2.length(); j++) {
            dp[0][j] = j;
        }

        // 4.遍历
        for (int i = 1; i <= word1.length(); i++) {
            for (int j = 1; j <= word2.length(); j++) {
                if (word2.charAt(j - 1) == word1.charAt(i - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j] + 1, Math.min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1));
                }
            }
        }
        return dp[word1.length()][word2.length()];
    }

3- ACM 实现

public class minDistance {

    public static int minDistance(String word1,String word2){
        // 1.定义dp
        // dp[i][j] 代表 以 i-1 和 j-1 为结尾的 word1 和 word2 的编辑距离
        int len1 = word1.length();
        int len2 = word2.length();
        int[][] dp = new int[len1+1][len2+1];

        // 2.递推公式
        // 相等的话 dp[i][j] = dp[i-1][j-1]
        // 不相等 删除两种 dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1

        // 3.初始化
        //
        for(int i = 1 ; i <= len1 ;i++){
            dp[i][0] = i;
        }
        for(int i = 1 ; i <= len2;i++){
            dp[0][i] = i;
        }

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


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String word1 = sc.nextLine();
        String word2 = sc.nextLine();
        System.out.println("结果是"+minDistance(word1,word2));
    }
}

http://www.kler.cn/news/309519.html

相关文章:

  • vue2制作高复用页面
  • 系统架构师考试学习笔记第五篇——架构设计补充知识(25)专业英语
  • Spring部分常见面试题
  • 关于Spring Cloud Gateway中 Filters的理解
  • 健身房预约小程序定制搭建,数字化运营管理
  • Python+Pytest框架,“api_key.py文件怎么编写“?
  • 【乐企-业务篇】生成发票左上角二维码
  • Linux和C语言(Day 12)
  • 华南医电科技集团受邀出席中马建交50周年高级别经贸合作交流活动
  • [Redis] Redis中的set和zset类型
  • 云轴科技ZStack 获鲲鹏应用创新大赛2024上海赛区决赛一等奖
  • vue3-print打印eletable某一行的数据
  • TI AM62X Secure Boot 流程简述
  • (黑马点评)六、好友关注系列功能实现
  • C语言 | Leetcode C语言接雨水II
  • vscode中前端项目文件格式化备份
  • Apple M3编译OpenSSL安卓平台SO库
  • django orm查询优化
  • Unity常用随机数算法
  • linux-Shell 编程-常用 Shell 脚本技巧
  • Python--数据格式转换
  • 在react中 使用redux
  • ubuntu安装wordpress(基于LNMP环境)
  • GBase8c主备版500升级步骤
  • 演示:基于WPF自绘的中国省份、城市、区县矢量地图
  • android 识别设备是否为模拟器
  • MySQL 按照条件(分组)只取一个形成列表 group max
  • PostgresML:通过 PostgreSQL 集成简化 AI 模型部署
  • git reset 几点疑问
  • 50ETF期权可以当天买卖吗?