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 ...
随机推荐
- 玩转微信2次开发1_交互通信api.php(微擎版)
在2次开发中,涉及到比较多的也比较繁琐的就是服务器和微信服务器的交互 用户在公众号里操作回复关键词都会让微信服务器和开发者的服务器进行交互 用户一旦关注了某某公众号--微信后台会去查询该公众号是否连接 ...
- 关于在scrapy中使用xpath
1. 还是以虎嗅为例,他给我返回的是一个json格式的json串 2.那么我需要操作的就是把json串转换成我们的字典格式再进行操作 str=json.loads(response.body)['da ...
- 3---Django rest framework源码分析(3)----节流
Django rest framework源码分析(3)----节流 目录 添加节流 自定义节流的方法 限制60s内只能访问3次 (1)API文件夹下面新建throttle.py,代码如下: # u ...
- AUTO Uninstaller【教程】AUTODESK系列软件MAYA,3DSMAX,CAD,INVENTOR,REVIT修复卸载工具 Windows x64位
小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...
- Docker镜像命名解析
镜像是Docker最核心的技术之一,也是应用发布的标准格式. 无论你是用docker pull image,或者是在Dockerfile里面写FROM image, 从Docker官方Registry ...
- (转)超全整理!Linux性能分析工具汇总合集
超全整理!Linux性能分析工具汇总合集 原文:http://rdc.hundsun.com/portal/article/731.html 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望, ...
- 内置组件 && vue中强大的缓存机制之keep-alive
vue中强大的缓存机制之keep-alive 最近在用vue做项目,在切换页面时发现切换回原来的页面无法保存原来的状态. 如A页面需要ajax请求数据,然后切换到B页面做某些事情,再切换回A页面时,A ...
- 客户端与服务器cookie
认识cookie 第一部分: 概要 cookie是一种早期使用的客户端存储机制(现在仍在广泛使用),cookie数据会在Web浏览器和Web服务器之间传输, 因为早期cookie是针对服务器脚本设计的 ...
- unity烘焙记录
1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...
- Python编码规范杂记(很乱:))
Python编码规范 导入模块 每一个(第三方)模块的导入都需要两个import语句, 如下 使用import some的方式导入模块, 如果有还有子模块的话, 则from father.son im ...