leetcode-62&63 Unique Paths I & II

来源:互联网 发布:matlab绘制矩阵灰度图 编辑:程序博客网 时间:2024/05/16 02:20

一,第62题,unique paths I
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).

How many possible unique paths are there?
这里写图片描述

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

题目简单,只是最基础的排列组合题型。
注意陷阱:当m、n很大的时候,结果会溢出!需要采用long int 保存结果。
时间为2ms;

/*思考:假设为m x n矩阵;由于只能向下或向右走,所以从start到finish只在纵向走(m-1)步,横向走(n-1)步,就行。也就是在(m+n-2)步中选(m-1)步纵向走即可。*/class Solution {public:    int uniquePaths(int m, int n) {        int re=0;//保存结果        if(m <= 0 || n <= 0)//去除无效输入        re=0;        else if( m==1 || n==1)//特殊情况        re = 1;        else        {            int max = (m-1) >= (n-1) ? (m-1) : (n-1);            long int sum=1;//注意这里采用的是long int 型            int k=1;            for(int i=(m+n-2); i > max; --i)            {                sum = sum * i ;                 if(!(sum % (i-max)))//如果可以除尽                sum /= (i-max);                else                {                    k *= (i-max);                }            }            sum /= k;            re = sum;        }        return re;    }};

二,第63题,unique paths II
Follow up for “Unique Paths”:

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.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

Note: m and n will be at most 100.

我自己想到的是采用回溯的办法,但是,实现很困难,特别是区别不重复的路径,很麻烦。如果采用上一题的思路,如果障碍少还可以,障碍多了,就非常麻烦,晕乎乎的。
纠结郁闷之余,去discuss看了看别人的方法,大惊失色。
这道题采用了一个很巧妙的方法。
就是采用递归的办法。由于只能沿着东方和南方两个方向递进,所以采用递归。
以下是参考别人的代码,引用其中的经典代码。
https://leetcode.com/discuss/13965/my-c-dp-solution-very-simple
其中方法一的代码最简洁。
方法二,三和方法一思路一致,只是细节处理不同。
方法二,采用倒推,额外空间缩小为只有一行即可;
方法三,与方法一几乎一样,只是方法一更加简洁。
总之都是好方法!
哎,路漫漫其修远兮……

class Solution {public:    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {    //方法一        int m = obstacleGrid.size() , n = obstacleGrid[0].size();        vector<vector<int>> dp(m+1,vector<int>(n+1,0));        dp[0][1] = 1;        for(int i = 1 ; i <= m ; ++i)            for(int j = 1 ; j <= n ; ++j)                if(!obstacleGrid[i-1][j-1])                    dp[i][j] = dp[i-1][j]+dp[i][j-1];        return dp[m][n];        /*        //方法二        int m = obstacleGrid.size();            int n = obstacleGrid[0].size();            if(obstacleGrid[m-1][n-1] == 1 || obstacleGrid[0][0] == 1)                return 0;            int res[n];            res[n-1] = 1;            for(int i = n-2; i >= 0; --i){                if(obstacleGrid[m-1][i] == 1)                    res[i] = 0;                else                    res[i] = res[i+1];            }            for(int i = m-2; i >= 0; --i){                for(int j = n-1; j >=0; --j){                    if(j == n-1){                        if(obstacleGrid[i][j] == 1)                            res[j] = 0;                    }else{                        if(obstacleGrid[i][j] == 1)                            res[j] = 0;                        else                            res[j] += res[j+1];                    }                }            }            return res[0];        */        /*        //方法三        int row=obstacleGrid.size();    int col=obstacleGrid[0].size();    vector<vector<int>> k(row,vector<int>(col,0));    if(obstacleGrid[0][0]==1||obstacleGrid[row-1][col-1]==1)    return 0;    else    k[0][0]=1;    for(int i=1;i<row;++i)    if(!obstacleGrid[i][0])    k[i][0]=k[i-1][0];    for(int j=1;j<col;++j)    if(!obstacleGrid[0][j])    k[0][j]=k[0][j-1];    for(int i=1;i<row;++i)    for(int j=1;j<col;++j)    {            if(!obstacleGrid[i][j])            k[i][j]=k[i-1][j]+k[i][j-1];    }    return k[row-1][col-1];        */    }};
0 0
原创粉丝点击