62. Unique Paths

My Submissions

Question
Total Accepted: 75227 Total
Submissions: 214539 Difficulty: Medium

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.

Subscribe to see which companies asked this question

Show Similar Problems

分析:

思路首先:令从(1。1)到(m,n)的最大走法数为dp[m,n]

不论什么一个点都是从上面走下来和从右边走过来两种可能的和

显然dp[m,n]=dp[m-1,n]+dp[m,n-1]

最简单的动态规划问题...........时间复杂度O(M*N)。空间复杂度O(M*N)

class Solution {
public:
int uniquePaths(int m, int n) {
vector< vector<int> > result(m+1);
for(int i=0;i <=m ;i++)
result[i].resize(n+1);//设置数组的大小m+1行,n+1列
for(int i=1;i<=n;i++)
result[1][i]=1;
for(int i=1;i<=m;i++)
result[i][1]=1;
for(int i=2;i<=m;i++)
for(int j=2;j<=n;j++)
result[i][j]=result[i-1][j]+result[i][j-1];
return result[m][n];
}
};

63. Unique Paths II

My Submissions

Question
Total Accepted: 55136 Total
Submissions: 191949 Difficulty: Medium

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.

Subscribe to see which companies asked this question

Hide Similar Problems

(M) Unique Paths

分析:

思路首先:

此题与原问题相较,变得是什么?

1。此障碍物以下和右边将不在获得来自于此的数量,也能够理解为贡献为0

2。有障碍的地方也将无法到达(这一条開始时没想到。总感觉leetcode题目意思不愿意说得直接明了)。也就是说此点的可到达路劲数直接为0

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
vector< vector<int> > result(m+1);
for(int i=0;i <=m ;i++)
result[i].resize(n+1);//设置数组的大小m+1行。n+1列
//初始化一定要正确。否则错无赦
result[1][1]= obstacleGrid[0][0]==1? 0:1;
for(int i=2;i<=n;i++)
result[1][i]=obstacleGrid[0][i-1]==1?0:result[1][i-1];//由上一次来推到
for(int i=2;i<=m;i++)
result[i][1]=obstacleGrid[i-1][0]==1?0:result[i-1][1]; for(int i=2;i<=m;i++)
for(int j=2;j<=n;j++)
result[i][j]=obstacleGrid[i-1][j-1]==1?0:result[i-1][j]+result[i][j-1]; //一旦当前有石头就无法到达,直接置零 return result[m][n];
}
};

联动另外一个问题:

64. Minimum Path Sum

My Submissions

Question
Total Accepted: 62294 Total
Submissions: 183284 Difficulty: Medium

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.

Note: You can only move either down or right at any point in time.

Subscribe to see which companies asked this question

Show Similar Problems

分析:

非常显然的动态规划问题。

令从原点(1,1)到目的点(m,n)的最小路劲和为result[m,n] 

不论什么一个点的路劲和都是来自二维数组上一行的最小路劲和或者来自右一列的最小路劲和与当前位置的值相加的结果 显然result[m,n]=min(result[m-1,n]+grid[m,n],result[m,n-1]+grid[m,n]) 

注意初始化问题

class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int row=grid.size();//行
int col=grid[0].size();
vector< vector<int> > result(row);
for(int i=0;i <row ;i++)
result[i].resize(col,0);//设置数组的大小row行。col列
result[0][0]=grid[0][0];//初始化
for(int i=1;i<col;i++)//初始化第一行
result[0][i]=result[0][i-1]+grid[0][i];
for(int i=1;i<row;i++)//初始化第一列
result[i][0]=result[i-1][0]+grid[i][0];
for(int i=1;i<row;i++)//计算中间结果
for(int j=1;j<col;j++)
result[i][j]=min(result[i][j-1]+grid[i][j],result[i-1][j]+grid[i][j]);
return result[row-1][col-1];
}
};

注:本博文为EbowTang原创。兴许可能继续更新本文。

假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50485468

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

&lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)的更多相关文章

  1. 【leetcode】62.63 Unique Paths

    62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...

  2. 62. 63. Unique Paths 64. Minimum Path Sum

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

  3. LeetCode OJ:Unique Paths(唯一路径)

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

  4. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

  5. LeetCode Unique Paths (简单DP)

    题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新 ...

  6. LeetCode OJ:Range Sum Query - Immutable(区域和)

    Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -&g ...

  7. LeetCode OJ:Search for a Range(区间查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  9. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

随机推荐

  1. ZOJ QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  2. Vue的style与class

    1. style 可以通过 :style="{height:`${heightData.main}px`}" 实现样式的动态绑定, style绑定的是一个对象,多个样式时用“,”隔 ...

  3. 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II

    洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...

  4. linux下多进程的文件拷贝与进程相关的一些基础知识

    之前实现了用文件IO的方式能够实现文件的拷贝,那么对于进程而言,我们是否也能够实现呢? 答案是肯定的. 进程资源: 首先我们先回想一下,进程的执行须要哪些资源呢?其资源包含CPU资源,内存资源,当然还 ...

  5. 鸟哥的Linux私房菜-----16、程序与资源管理

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  6. 理解String的compareTo()方法返回值

    compareTo()的返回值是整型,它是先比较对应字符的大小(ASCII码顺序), 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值. 如果第一个字符和参数的第一个字符相等,则以第 ...

  7. node的express参数获取

    1.express中的不定向路由参数的获取 如: app.get('/profile/:id', function(req, res) { var id=req.params.id res.send( ...

  8. Vim 模式及常用命令整理

    VIM 命令     以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令.   vim的模式 基本上可以分为3种模式,分别是命令模式(command mode).插入模 ...

  9. comparator接口与Comparable接口的差别

    1. Comparator 和 Comparable 同样的地方 他们都是java的一个接口, 而且是用来对自己定义的class比較大小的, 什么是自己定义class: 如 public class  ...

  10. 3dmax入门

    动画 自己主动关键帧 设置关键帧 路径绑定 材质M打开 渲染f10 骨骼绑定. ..