1.

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.

class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> v(m, vector<int>(n, ));
int i, j;
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if( == i || == j)
v[i][j] = ;
else
v[i][j] = v[i-][j] + v[i][j-];
}
}
return v[m-][n-];
}
};

2.

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.

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if(m <= )
return ;
int n = obstacleGrid[].size();
if(n <= || obstacleGrid[][] == )
return ;
vector<vector<int>> v(m, vector<int>(n, ));
int i, j;
for(i = ; i < m; i++)
{
for(j = ; j < n; j++)
{
if(obstacleGrid[i][j] == )
v[i][j] = ;
else if( == i && j)
v[i][j] = v[][j-];
else if( == j && i)
v[i][j] = v[i-][];
else if(i && j)
v[i][j] = v[i-][j] + v[i][j-];
else
v[i][j] = ;
}
}
return v[m-][n-];
}
};

3.

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.

int minPathSum(vector<vector<int> > &grid) {
if (grid.size()<=){
return ;
}
int i, j;
for(i=; i<grid.size(); i++){
for(j=; j<grid[i].size(); j++){
int top = i-< ? INT_MAX : grid[i-][j] ;
int left = j-< ? INT_MAX : grid[i][j-];
if (top==INT_MAX && left==INT_MAX){
continue;
}
grid[i][j] += (top < left? top: left);
}
}
return grid[grid.size()-][grid[].size()-];
}

62. 63. Unique Paths 64. Minimum Path Sum的更多相关文章

  1. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  2. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  3. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  4. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  5. 【一天一道LeetCode】#64. Minimum Path Sum.md

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【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 ...

  7. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  8. [LeetCode] 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 ...

  9. [LC] 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 ...

随机推荐

  1. Linux服务器上Tomcat的Web工程部署

    Linux服务器上Tomcat的Web工程部署 部署Web应用到Tomcat服务器就是将开放好的JavaWeb应用打包成war包,然后发布到tomcat服务器的webapps目录下: 步骤1,先进入t ...

  2. 20145122《敏捷开发与XP实践 》实验三实验报告

    实验名称 敏捷开发与XP实践 实验内容 1.团队代码要使用git在实验楼中托管,要使用结对同学中的一个同学的账号托管. 2.使用git推送代码并对结对同学的代码修改完成后再git推送. 3.掌握重构流 ...

  3. linux内核分析 第一周

    计算机是如何工作的 冯·诺依曼理论的要点是: 数字计算机的数制采用二进制:计算机应该按照程序顺序执行. 冯·诺依曼体系结构 根据冯·诺依曼体系结构构成的计算机,必须具有如下功能:把需要的程序和数据送至 ...

  4. java在访问https资源时的证书信任问题

    java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...

  5. warning C4018: “<”: 有符号/无符号不匹配

    原因: 将两个不同的类型进行了比较,如: int a:unsigned short b: if(a>b)... 解决:改为同一种类型

  6. BZOJ1307: 玩具 单调队列

    Description 小球球是个可爱的孩子,他喜欢玩具,另外小球球有个大大的柜子,里面放满了玩具,由于柜子太高了,每天小球球都会让妈妈从柜子上拿一些玩具放在地板上让小球球玩. 这天,小球球把所有的N ...

  7. C#学习笔记(十四):多态、虚方法和抽象类

    虚方法/非虚方法 < 实例方法 = 非静态方法 = 非类方法(非实例方法 = 静态方法 = 类方法) 函数签名(参数列表,或参数列表 + 返回类型) using System; using Sy ...

  8. Spring资源加载基础ClassLoader

    1 ClassLoader工作机制 1.1 ClassLoader作用 寻找类字节码文件并构造出类在JVM内部表示的组件.负责运行时查找和装入Class字节码文件 1.2 装载步骤 1.2.1 装载 ...

  9. js函数事件对象

    每个函数都有4个默认对象 arguments 保存着实际传入的参数,集合列表 return 有两个功能,打断函数和返回函数值 this 谁调用的函数,this就是谁 event 事件对象 事件 box ...

  10. HDU 1757 A Simple Math Problem(矩阵快速幂模板)

    题意:题意很简单,不多说了. 思路: |f(10) |       |a0 a1 a2 ...a8 a9|    |f(9)|| f(9)  |       | 1   0   0 ... 0     ...