Leetcode: Edit Distance、(week10---hard×1, medium×2)

72. Edit Distance

题目

分析

题解

63. Unique Paths II

题目

分析

题解

64. Minimum Path Sum

题目

分析

题解 


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:

  1. Insert a character
  2. Delete a character
  3. Replace a character

分析

  1. 这一道题其实也就是老师上课讲过的知识,也就是编辑距离的问题。主要也就是三种编辑方式:
    1. 插入 ---消耗为1
    2. 删除 ---消耗为1
    3. 替换 ---当对应的位置字符相同的时候则消耗为0,对应的位置字符不同时则消耗为1
  2. 这个问题可以分解为子问题,也就是在前者的基础上进行解答
  3. 假设两个字符串分别为 str1, str2,长度分别为l1与l2,创建一个数组为array[l1+1][l2+1]注意有+1,这非常关键
    1. array[i+1][j+1]的含义在于记录字符串str1[0 - i]字串与str2[0-j]的最小的编辑距离
    2. 同时当i或者j为0的时候,可以很容易的知道array[i][0] = i    array[0][j] = j
    3. 同时上面的三种编辑方式可以递推表示为:  

array[i][j] = min{1 + array[i-1][j], array[i][j - 1] + 1, !(str1[i-1] == str2[j-1]) + array[i-1][j-1]}

上面大括号中分别表示三种方式的消耗,只需要取最小值就好了(注意C++中min函数的参数只能是两个,否则会出现错误)

题解

class Solution {
public:
    int minDistance(string word1, string word2) {
        int l1 = word1.length();
        int l2 = word2.length();
        if(l1 == 0) return l2;
        if(l2 == 0) return l1;
        int array1[l1+1][l2+1];
        for(int i = 0; i <= l1; i++){
            array1[i][0] = i;
        }
        for(int i = 1; i <= l2; i++){
            array1[0][i] = i;
        }
        int judge;
        for(int i = 1; i <= l1; i++){
            for(int j = 1; j <= l2; j++){
                judge = (word1[i - 1] == word2[j - 1] ? 0 : 1);
                array1[i][j] = min((array1[i][j - 1] + 1), (array1[i - 1][j] + 1));
                array1[i][j] = min(array1[i][j], judge + array1[i - 1][j - 1]);
            }
        }
        return array1[l1][l2];
    }
};

63. Unique Paths II

题目

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

分析

  1. 这一道题目是为了寻找从左上角到右下角的总的路径的数目,因为规定了路的方向,也就是只有向右与向下
  2. 所以可以创建一个二维的数组,用于记录到当前位置的路径的数目,这一个数组可以从每一行开始遍历也可以从每一列开始遍历。
  3. 当当前位置是障碍物时则路径数目为0,反之则为 sum(array[i - 1][j] + array[i][j - 1])

题解

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
            int row = obstacleGrid.size();
            if(row == 0) return 0;
            if(obstacleGrid[0][0] == 1)return 0;
            int col = obstacleGrid[0].size();
            int nums[row][col];
            nums[0][0] = 1;
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    if(i == 0 && j == 0) continue;
                    nums[i][j] = 0;
                    if(obstacleGrid[i][j] == 1) continue;
                    if(i != 0) nums[i][j] += nums[i - 1][j];
                    if(j != 0) nums[i][j] += nums[i][j - 1];
                }
            }
            return nums[row-1][col-1];
    }
};

64. Minimum Path Sum

题目

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

分析

这一题与上面的类似,不同点只是在于每一次取最大值

题解 

class Solution {
public:
    int minPathSum(vector<vector<int>>& obstacleGrid) {
            int row = obstacleGrid.size();
            if(row == 0) return 0;
            int col = obstacleGrid[0].size();
            int nums[row][col];
            nums[0][0] = obstacleGrid[0][0];
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    if(i == 0 && j == 0) continue;
                    nums[i][j] = 0;
                    if(i != 0 && j != 0) {
                        if(nums[i][j-1] > nums[i - 1][j]) 
                        nums[i][j] = nums[i - 1][j];
                        else 
                        nums[i][j] = nums[i][j - 1];
                    }
                    else if(j == 0) nums[i][j] = nums[i - 1][j];
                    else if(i == 0) nums[i][j] = nums[i][j - 1];
                    nums[i][j] += obstacleGrid[i][j];
                }
            }
            return nums[row-1][col-1];
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值