Unique Paths

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.

思路: 其实答案就是 C(m+n-2, m-1). 但是写程序利用动态规划会简单快捷。(给两个代码,第一个方便理解,第二个是基于第一个的优化)

1.

class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
vector<vector<int> > times(m, vector<int>(n, 0));
for(int r = 0; r < m; ++r) times[r][0] = 1;
for(int c = 1; c < n; ++c) times[0][c] = 1; // 只能到 1 次
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
times[r][c] = times[r-1][c] + times[r][c-1];
return times[m-1][n-1];
}
};

2.

class Solution { // C(m+n-2, m-1)
public:
int uniquePaths(int m, int n) {
if(m <= 0 || n <= 0) return 0;
vector<int> R(n, 1); // 一行行的记录
for(int r = 1; r < m; ++r)
for(int c = 1; c < n; ++c)
R[c] = R[c]+ R[c-1];
return R[n-1];
}
};

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.

思路:同上,只是最初初始化全 0 . 当前位置为 1 时,则当到达前位置的步数为 0.

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(!obstacleGrid.size() || !obstacleGrid[0].size()) return 0;
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<int> R(n, 0);
R[0] = 1-obstacleGrid[0][0];
for(int r = 0; r < m; ++r)
for(int c = 0; c < n; ++c) {
if(c > 0)
R[c] = (obstacleGrid[r][c] == 1 ? 0 : (R[c] + R[c-1]));
else if(obstacleGrid[r][c] == 1) R[0] = 0;
}
return R[n-1];
}
};

61. Unique Paths && Unique Paths II的更多相关文章

  1. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  2. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  3. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  4. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

  6. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  7. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  8. leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  9. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. 使用Jsoup 抓取页面的数据

    需要使用的是jsoup-1.7.3.jar包   如果需要看文档我下载请借一步到官网:http://jsoup.org/ 这里贴一下我用到的 Java工程的测试代码 package com.javen ...

  2. asp.net mvc处理css和js版本问题

    当服务的修改了js和css内容后,发布到IIS服务器上,总是导致客户端内容显示不正确,原因是客户端存在缓存,还是加载的原来的js和css问题. 在css或js后面添加版本号,例如: <scrip ...

  3. iOS iPad开发之UIPopoverController的使用

    1. 什么是UIPopoverController? 是iPad开发中常见的一种控制器(在iphone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIViewCon ...

  4. Centos上的安装openoffice+unoconv+swftools (转)

    ############################## #    swftools的安装     # ############################## 1.安装所需的库和组件 yum ...

  5. 如何取消win10电脑自动更新

    windows 10系统中关闭windows自动更新步骤如下:1.按键盘上的“Windows徽标键+R”组合键,可以调出“运行”窗口. 2.输入gpedit.msc,单击“确定”,可以打开“本地组策略 ...

  6. GCD的其他方法

    1.栅栏函数 作用:控制线程的执行顺序 注:栅栏函数不能使用全局并发队列 -(void)barrier { //1.创建队列(并发队列) dispatch_queue_t queue = dispat ...

  7. Linux中查看文件编码

    在Linux中查看文件编码可以通过以下几种方式:1.在Vim中可以直接查看文件编码:set fileencoding即可显示文件编码格式.如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱 ...

  8. Android应用程序“R文件”消失

    其实Android自己维护这一个 public final class R类主要是跟新资源文件,这个R.java无需我们自己去修改,如果你不了解千万不要去修改它,它定义的每个资源值都是唯一的,不会和系 ...

  9. ios学习之路

    整合网络资源和自己的实际经验,表述于此. 1 Xcode详解 非常实用的xcode介绍,详情请见外部链接http://demo.netfoucs.com/shulianghan/article/det ...

  10. 极值问题(acms)

    [问题描述] 已知m.n为整数,且满足下列两个条件: ① m.n∈{1,2,…,k},即1≤m,n≤k,(1≤k≤109). ②(n2-m*n-m2)2=1 你的任务是:编程输入正整数k,求一组满足上 ...