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. HTTP 超时

    TWinHTTPTimeouts = class(TPersistent) private FConnectTimeout, FReceiveTimeout, FSendTimeout: DWord; ...

  2. web.xml的初始化参数

    web.xml的初始化参数 ---------首先声明,这里所介绍的是web中context-param,init-param参数的初始化配置---------- ------------------ ...

  3. c#基础知识-2

    1.在控制台接受数据时可以这样输入: using System; using System.Collections.Generic; using System.Linq; using System.T ...

  4. 如何重新安装DEDECMS织梦系统

    重装的方法: 1.找到安装目录\install\index.php.bak文件,改名为index.php:   2.删除安装目录\install\install_lock文件:

  5. Android--JUnit单元测试

      Android--JUnit单元测试 前言 本篇博客说明一下在Android开发中,如何使用JUnit进行单元测试.首先来了解一下什么是JUnit,JUnit测试是白盒测试,即主要是程序员自己对开 ...

  6. Python的平凡之路(3)

     一.函数基本语法及特性 面向对象:(华山派)—类 —class 面向过程:(少林派)—过程 —df 函数式编程:逍遥派    —函数— df 一般的,在一个变化过程中,如果有两个变量x和y,并且对于 ...

  7. HDU 4352 XHXJ's LIS

    奇妙的题. 你先得会另外一个nlogn的LIS算法.(我一直只会BIT.....) 然后维护下每个数码作为结尾出现过没有就完了. #include<iostream> #include&l ...

  8. BZOJ 3110 树套树 && 永久化标记

    感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...

  9. 【转】15个无比华丽的HTML5/CSS3动画应用

    原文转自:http://www.html5cn.org/article-7089-1.html 前几天,HTML5标准已经尘埃落定,未来的Web将会是由HTML5主导,当然作为开发者对这一喜讯更为动心 ...

  10. WampServe修改默认网站目录的方法(转)

    1wamp简介 WampServe集成了Apache.MySQL.PHP.phpmyadmin,支持Apache的mod_rewrite,PHP扩展.Apache模块只需要在菜单“开启/关闭”上点点就 ...