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 ...
随机推荐
- Yii2 hasMany 关联后加条件
当前模型类为活动表id,关联评论表的type_id,条件是评论表的type要等于2public function getComment(){ return $this->hasMany(Comm ...
- zookeeper+dubbo简单部署方案
1.zookeeper服务 首先,我们需要下载:zookeeper-3.4.9 解压后,在conf文件夹中添加一个配置文件zoo.cfg,内容如下: tickTime=2000 dataDir=/Us ...
- Java基础23-main方法
/* 主函数(主方法) 1.public(访问修饰符,公共的)代表类或者该函数访问权限是最大的 2.static 代表主函数随着类的加载就已经存在了 3.void 代表主函数没有具体的返回值 4.ma ...
- (转) 来自: http://man.linuxde.net/tee
tee命令文件过滤分割与合并 tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin.简单的说就是把数据重定向到给定文件和屏幕上. 存在缓存机制,每1024个 ...
- HikariCP配置使用spring结合--Java数据库连接池
我的个人德州扑克项目https://github.com/mingzijian/pokers,欢迎给星星.maven引入: Java 8 maven artifact: <dependency& ...
- Kettle集群部署(1台Windows主机和2台Linux服务器)
不多说,直接上干货! http://blog.csdn.net/jianglushou9763/article/details/70859616
- 0.数据结构(python语言) 基本概念 算法的代价及度量!!!
先看思维导图: *思维导图有点简陋,本着循循渐进的思想,这小节的知识大多只做了解即可. *重点在于算法的代价及度量!!!查找资料务必弄清楚. 零.四个基本概念 问题:一个具体的需求 问题实例:针对问题 ...
- mysql 数据库8.0版本,jdbc驱动连接问题
前言 8.0版本的mysql数据的连接 与 5.0的有所不同,下面直接贴出 8.0版本应该有的 jdbc驱动连接,还有 mysql 的jdbc jar包要8.0以上的 内容如下 : jdbc.dri ...
- C++程序设计基础(3)条件语句和循环语句
注:读<程序员面试笔记>笔记总结 1.知识点 1.1条件语句 (1)if……:(2)if……else……:(3)if……else if……:(4)switch(){case ():brea ...
- vue——指令系统
指令系统,可以联想咱们的cmd命令行工具,只要我输入一条正确的指令,系统就开始干活了. 在vue中,指令系统,设置一些命令之后,来操作我们的数据属性,并展示到我们的DOM上. 在vue中提供了一套为数 ...