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). ...
随机推荐
- idea tomcat debug不能启动的问题
显示:Connected to the target VM, address: '127.0.0.1:54692', transport: 'socket' 其实原因是没调整好面板.晕 ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- 《专访 RocketMQ 联合创始人:项目思路、技术细节和未来规划》
专访 RocketMQ 联合创始人:项目思路.技术细节和未来规划 木环 阅读数:138092017 年 2 月 20 日 18:00 编者按 这些年开源氛围越来越好,各大 IT 公司都纷纷将一 ...
- CF1263F Economic Difficulties(DP)
拿小号打了这场,然而做到这里时少看了条件,最后 10min 才发现,没有 AK,身败名裂-- 赛后看就是 sb 题-- (好像这题也不值 2500 吧?) 首先注意到一条很重要的条件:对于每棵树,都存 ...
- 《Effective-Ruby》读书笔记
本篇是在我接触了 Ruby 很短一段时间后有幸捧起的一本书,下面结合自己的一些思考,来输出一下自己的读书笔记 前言 学习一门新的编程语言通常需要经过两个阶段: 第一个阶段是学习这门编程语言的语法和结构 ...
- centos--该虚拟机似乎正在使用中。 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权。否则,请按“取消(C)”按钮以防损坏。
centos非正常关机,导致无法正常启动的问题 该虚拟机似乎正在使用中. 如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权.否则,请按“取消(C)”按钮以防损坏. 解决方案: 1. 找 ...
- pytest框架之mark标签
对测试用例打标签,在运行测试用例的时候,可根据标签名来过滤要运行的用例. 一.注册标签名 1.创建pytest.ini文件,在文件中按如下方式添加标签名: [pytest] markers = smo ...
- ansible小结(八)ansible-playbook简单使用
ansbile-playbook是一系统ansible命令的集合,其利用yaml 语言编写,运行过程,ansbile-playbook命令根据自上而下的顺序依次执行.同时,playbook开创了很多特 ...
- OpenGL光照3:光源
本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...
- C# Task ContinueWith
static void Main(string[] args) { Task firstTask = Task.Run(() => { PrintPlus(); }); Task secondT ...