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).
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:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3
Output: 28
题目大意:
从m乘n矩阵左上角走到右下角,每一步只能向右或向下,求共有多少条不同路径。
递归解决:
class Solution {
public:
vector<vector<int>> v; int solve(int m, int n) {//从(m, n)到(1, 1)有多少条路径
if (m == || n == )
return ;
if (v[m][n] >= )
return v[m][n];
v[m][n] = solve(m - , n) + solve(m, n - );
return v[m][n];
} int uniquePaths(int m, int n) {
if (m == || n == )
return ;
if (m == || n == )
return ;
v.resize(m + , vector<int>(n + , -));
return solve(m - , n) + solve(m, n - );
}
};
迭代:
class Solution {
public: int uniquePaths(int m, int n) {
if (m == || n == )
return ;
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 - ];
}
};
leetcode 62、Unique Paths的更多相关文章
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- 【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 ...
- 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). ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【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 ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- [转] SpringBoot RESTful 应用中的异常处理小结
[From] https://segmentfault.com/a/1190000006749441 SpringBoot RESTful 应用中的异常处理小结 永顺 2016年08月29日发布 赞 ...
- RMQ、POJ3264
这里说几篇博客,建议从上到下看 https://blog.csdn.net/qq_31759205/article/details/75008659 https://blog.csdn.net/sgh ...
- C语言中的输入方式
在c语言中,有gets().scanf().getchar()等输入方式,但是不同的方式处理的方式不同. scanf()读取时遇见tab.space.enter时会结束读取,不会舍弃最后的回车符(即回 ...
- js判断文件是否存在的方法
在做电力监控项目的时候,有一个需求就是左右布局的框架,点击左边的图形文件地址,然后去文件夹中找到文件,再在右边出现对应的图形文件,但是有些文件可能是配置的时候有问题,找不到文件,所以js需要判断,以下 ...
- Java学习第二十天
1:递归(理解) (1)方法定义中调用方法本身的现象 举例:老和尚给小和尚讲故事,我们学编程 (2)递归的注意事项: A:要有出口,否则就是死递归 B:次数不能过多,否则内存溢出 C:构造方法不能递归 ...
- 【VMware&Vritualbox】虚拟机安装windows server2016
一.下载镜像 参考链接:https://blog.csdn.net/yenange/article/details/52981769 http://blog.sina.com.cn/s/blog_10 ...
- Sql批处理语句
同时写3个批处理,如果前2个批处理没有问题,最后一个有错误那么3个批处理都不会执行需要注意列如: use Materl GO select * from t_icitem GO inset into ...
- Java复习之Eclipse快捷键大全
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- Drupal theme_hook
模板语言和主题引擎 用Drupal的行话来说,主题就是一组负责你站点外观的文件.你可以从http://drupal.org/project/Themes下载第 3方主题,或者你可以自己动手创建一个主题 ...
- ie浏览器 vuejs axios Promise 未定义
随着前端技术的发现,es6语法在被更大范围的使用,而很多的浏览器并不支持ES6,比如IE…… 这里我们介绍几个解决方法. 一.使浏览器兼容ES6基本语法 1.在引入其他脚本前先引入browser.mi ...