LeetCode 62. Unique Paths不同路径 (C++/Java)
题目:
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的网格,从左上角走到右下角,只能往下前进一格或往右前进一格,共有多少种走法。
到(m,n)格的路径数等于到(m-1,n)的路径数加上(m,n-1)的路径数,根据这个我们可以通过递推求得结果,机器人初始的位置路径数等于1,注意边界条件的判定,也可以将二维数组多开辟一行一列,用来跳过边界条件的处理,还可以先将第一行和第一列都初始化为1,再进行递推求解。
此外我们还可以通过递归求解此问题,即uniquePaths[m][n] = uniquePaths(m-1, n) + uniquePaths(m, n-1),只不过我们不是从起点求到终点,而是最先求右下角的路径数通过递归求解,同样也要注意递归终止条件。
程序:
C++
//Solution 1
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> res(m+, vector<int>(n+, ));
for(int i = ; i < m+; ++i){
for(int j = ; j < n+; ++j){
if(i == && j == )
res[][] = ;
else
res[i][j] = res[i-][j] + res[i][j-];
}
}
return res[m][n];
}
};
//Solution 2
class Solution {
public:
int uniquePaths(int m, int n) {
if(m < || n < ) return ;
if(m == && n == ) return ;
if(res[m][n] > ) return res[m][n]; res[m][n] = uniquePaths(m-, n) + uniquePaths(m, n-);
return res[m][n];
}
private:
int res[][]={};
};
Java
class Solution {
public int uniquePaths(int m, int n) {
int[][] res = new int[m+1][n+1];
for(int i = 1; i < m+1; ++i)
for(int j = 1; j < n+1; ++j){
if(i == 1 && j == 1){
res[1][1] = 1;
}
else{
res[i][j] = res[i-1][j] + res[i][j-1];
}
}
return res[m][n];
}
}
class Solution {
private int[][] res = new int[101][101];
public int uniquePaths(int m, int n) {
//int[][] res = new int[m+1][n+1];
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
if(res[m][n] > 0) return res[m][n];
res[m][n] = uniquePaths(m-1, n) + uniquePaths(m, n-1);
return res[m][n];
}
}
LeetCode 62. Unique Paths不同路径 (C++/Java)的更多相关文章
- [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 ...
- [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 ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 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 ...
- [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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- 62. Unique Paths不同路径
网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- linux编程stat检测文件元数据信息
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...
- Vscode 打字特效插件Power Mode安装使用说明
壹 ❀ 引 我记得在17年使用atom编辑器的时候,使用过一款打字特效的插件,只要我们输入代码,代码上方就会有与代码颜色对应的星星效果,今天脑抽突然想起了这个中二插件,搜索了一番成功安装,大致效果如 ...
- SQLserver无法删除数据库 "XXXX",因为该数据库当前正在使用。
问题描述: 有时候删除库的时候,会显示无法删除数据库,因为该数据库当前正在使用. 解决方法: 方法一: EXEC msdb.dbo.sp_delete_database_backuphistory @ ...
- 09-Django静态文件
1.静态文件 项目中的图片.CSS.js都是静态文件,一般会将静态文件放到一个单独的目录下,也方便管理.一般会将静态文件放到一个单独的目录下,也可以放在应用的目录下,由于静态文件是全部应用都在使用的, ...
- 基于appium的模拟单点或多点触屏操作
一.单点触控 TouchAction类:将一系列的动作放在一个链条中,然后将该链条传递给服务器,服务器接受该链条后,解析各个动作,逐个执行,TouchAction类提供了以下几种方法: 短按:pres ...
- pytest框架优化——清理历史截图图片和allure报告文件
痛点分析: 当我们每次执行完用例的时候,如果出现bug或者是测试脚本出了问题,一般会通过测试报告.异常截图.日志来定位分析,但是我们发现运行次数多了之后,异常截图和测试报告会不停地增多,对我们定位分析 ...
- 基于python的selenium常用操作方法(2)
9 多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...
- 【51Nod1584】加权约数和(数论)
[51Nod1584]加权约数和(数论) 题面 51Nod 题解 要求的是\[\sum_{i=1}^n\sum_{j=1}^n max(i,j)\sigma(ij)\] 这个\(max\)太讨厌了,直 ...
- linux 安装 nvm, node.js, npm
vscode在wsl中开发node应用,如何安装nvm? git clone git@github.com:nvm-sh/nvm.git ~/.nvm 设置淘宝registry npm config ...
- yolov3和ssd的区别
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/BlowfishKing/article/d ...