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 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

  1. Input: m = 3, n = 2
  2. Output: 3
  3. Explanation:
  4. From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
  5. 1. Right -> Right -> Down
  6. 2. Right -> Down -> Right
  7. 3. Down -> Right -> Right

Example 2:

  1. Input: m = 7, n = 3
  2. Output: 28

题目大意:

从m乘n矩阵左上角走到右下角,每一步只能向右或向下,求共有多少条不同路径。

递归解决:

  1. class Solution {
  2. public:
  3. vector<vector<int>> v;
  4.  
  5. int solve(int m, int n) {//从(m, n)到(1, 1)有多少条路径
  6. if (m == || n == )
  7. return ;
  8. if (v[m][n] >= )
  9. return v[m][n];
  10. v[m][n] = solve(m - , n) + solve(m, n - );
  11. return v[m][n];
  12. }
  13.  
  14. int uniquePaths(int m, int n) {
  15. if (m == || n == )
  16. return ;
  17. if (m == || n == )
  18. return ;
  19. v.resize(m + , vector<int>(n + , -));
  20. return solve(m - , n) + solve(m, n - );
  21. }
  22. };

迭代:

  1. class Solution {
  2. public:
  3.  
  4. int uniquePaths(int m, int n) {
  5. if (m == || n == )
  6. return ;
  7. vector<vector<int>> v(m, vector<int>(n));
  8. int i, j;
  9. for (i = ; i < m; i++) {
  10. for (j = ; j < n; j++) {
  11. if (i == || j == )
  12. v[i][j] = ;
  13. else
  14. v[i][j] = v[i - ][j] + v[i][j - ];
  15. }
  16. }
  17. return v[m - ][n - ];
  18. }
  19. };

leetcode 62、Unique Paths的更多相关文章

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

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

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

  4. LeetCode(62)Unique Paths

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

  5. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  6. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  7. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  8. 【LeetCode练习题】Unique Paths

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

  9. LeetCode OJ 63. Unique Paths II

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

随机推荐

  1. .Net支持Redis哨兵模式

    csredis 博客 csRedisgit地址 csRedis3.2.1 Nuget地址 (在使用csredis3.2.1获取sentinel时产生运行时异常,调查问题最后发现是获取sentinel的 ...

  2. Linux下Tomcat启动报 The BASEDIR environment variable is not defined

    今天是2017年2月27.在Linux下部署Tomcat官网下载的Tomcat 8.5,结果启动startup.sh报如下错,即使只是跑version.sh也报同样的错. $ ./version.sh ...

  3. CNN Advanced

    from sys import path path.append('/home/ustcjing/models/tutorials/image/cifar10/') import cifar10,ci ...

  4. Ignite cahce 存储object类型数据和object类型数据序列化后string存储区别

    Ignite cache在存储时 object类型的数据和 序列化该object成string类型 两者存储时间差不多. 但是这两者在读取出来的时候,string类型比object类型快很多. 以下为 ...

  5. mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)

    背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...

  6. 使用jxl读取excel内容,并转换成Json,用于Datagrid

    一.上传excel文件,得到InputStream,由InputStream得到Jxl中的Workbook,取出内容,存到二维数组中. 1.使用 Jquery Uploadify 插件(http:// ...

  7. HDU 1754——I Hate It——————【线段树单点替换、区间求最大值】

    I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  8. css三角块

    html: <div class="angle"></div> css: .angle{ width: 0px; height: 0px; border-b ...

  9. [转]Session and application state in ASP.NET Core

    本文转自:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state By Rick Anderson and Steve ...

  10. git每次提交都输入密码

    打开gitbash执行即可 git config --global credential.helper store 长期储存密码,因为git默认是不储存密码的,不执行这条命令的话每次更新代码,或者提交 ...