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 ...
随机推荐
- .Net支持Redis哨兵模式
csredis 博客 csRedisgit地址 csRedis3.2.1 Nuget地址 (在使用csredis3.2.1获取sentinel时产生运行时异常,调查问题最后发现是获取sentinel的 ...
- Linux下Tomcat启动报 The BASEDIR environment variable is not defined
今天是2017年2月27.在Linux下部署Tomcat官网下载的Tomcat 8.5,结果启动startup.sh报如下错,即使只是跑version.sh也报同样的错. $ ./version.sh ...
- CNN Advanced
from sys import path path.append('/home/ustcjing/models/tutorials/image/cifar10/') import cifar10,ci ...
- Ignite cahce 存储object类型数据和object类型数据序列化后string存储区别
Ignite cache在存储时 object类型的数据和 序列化该object成string类型 两者存储时间差不多. 但是这两者在读取出来的时候,string类型比object类型快很多. 以下为 ...
- mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)
背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...
- 使用jxl读取excel内容,并转换成Json,用于Datagrid
一.上传excel文件,得到InputStream,由InputStream得到Jxl中的Workbook,取出内容,存到二维数组中. 1.使用 Jquery Uploadify 插件(http:// ...
- HDU 1754——I Hate It——————【线段树单点替换、区间求最大值】
I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- css三角块
html: <div class="angle"></div> css: .angle{ width: 0px; height: 0px; border-b ...
- [转]Session and application state in ASP.NET Core
本文转自:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state By Rick Anderson and Steve ...
- git每次提交都输入密码
打开gitbash执行即可 git config --global credential.helper store 长期储存密码,因为git默认是不储存密码的,不执行这条命令的话每次更新代码,或者提交 ...