Question

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?

Note: m and n will be at most 100.

Solution 1

动态规划,到(i, j)的所有方法等于到(i - 1, j)加上(i, j - 1)。

  1. class Solution {
  2. public:
  3. int uniquePaths(int m, int n) {
  4. vector<vector<int> > table(m, vector<int> (n, 1));
  5. for (int i = 1; i < m; i++) {
  6. for (int j = 1; j < n; j++) {
  7. table[i][j] = table[i - 1][j] + table[i][j - 1];
  8. }
  9. }
  10. return table[m - 1][n - 1];
  11. }
  12. };

Solution 2

组合数学中的格路问题,注意要边乘边除,不然要越界。

  1. class Solution {
  2. public:
  3. int uniquePaths(int m, int n) {
  4. int N = n + m - 2;// how much steps we need to do
  5. int k = m - 1; // number of steps that need to go down
  6. double res = 1;
  7. // here we calculate the total possible path number
  8. // Combination(N, k) = n! / (k!(n - k)!)
  9. // reduce the numerator and denominator and get
  10. // C = ( (n - k + 1) * (n - k + 2) * ... * n ) / k!
  11. for (int i = 1; i <= k; i++)
  12. res = res * (N - k + i) / i;
  13. return (int)res;
  14. }
  15. };

LeetCode——Unique Paths的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [LeetCode] 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: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [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 ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. Leetcode Unique Paths

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

随机推荐

  1. 解决IOS7在TableView 被导航栏挡住的BUG!!

    self.edgesForExtendedLayout = UIRectEdgeNone; 就这么简单!

  2. 具备双向通行能力的架构对于移动APP属于刚性需求。 WebSocket连接 注册信令

    双向通信使用指南_用户指南(开放 API)_API 网关-阿里云 https://help.aliyun.com/document_detail/66031.html 流程描述 (1) 客户端在启动的 ...

  3. 并发编程 - io模型 - 总结

    1.提交任务得方式: 同步:提交完任务,等结果,执行下一个任务 异步:提交完,接着执行,异步 + 回调 异步不等结果,提交完任务,任务执行完后,会自动触发回调函数2.同步不等于阻塞: 阻塞:遇到io, ...

  4. Git Extension工具安装及使用

    以下界面所示的三个工具,如果没安装过,则勾上让其安装.MsysGit为Git的Windows版本,必须要安装:Kdiff为对比/合并工具,可选安装,可以换为使用其它的相关工具:最后一个Windows ...

  5. DB_FILE_MULTIBLOCK_READ_COUNT对物理读和IO次数的影响

    当执行SELECT语句时,如果在内存里找不到相应的数据,就会从磁盘读取进而缓存至LRU末端(冷端),这个过程就叫物理读.当相应数据已在内存,就会逻辑读. 物理读是磁盘读,逻辑读是内存读:内存读的速度远 ...

  6. 工作笔记-javascript-网络层封装

    /** * @Author Mona * @Date 2016-12-08 * @description 网络层封装 */ /** * 封装基本请求方式 */ window.BaseRequest = ...

  7. linux用户 群组权限

    用户及passwd文件 /etc/passwd文件的功能 /etc/passwd文件每个字段的具体含义 shadow文件 /etc/shadow文件的功能 /etc/shadow文件每个字段的具体含义 ...

  8. C# DataTable Column DataType 对应 数据库

      public DataTable MakeDataTable(){ DataTable myTable; DataRow myNewRow; // Create a new DataTable. ...

  9. [笔记]Delphi 2007写DLL供VC调用实例

    考虑如下几种常用情况: - VC传入int,返回int- VC传入char *,返回int- VC传入char *,返回char *及int 为简化问题,传递的字符串参数只考虑ANSI格式,不考虑UN ...

  10. Restful概念

    文章节选自: http://www.ruanyifeng.com/blog/2011/09/restful https://www.zhihu.com/question/28557115/answer ...