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). ...
随机推荐
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- R-长尾词练习
一. 长尾关键词的特征 长尾关键词通常比较长,往往是2-3个词组成,甚至是短语,存在于内容页面,除了内容页的标题,还存在于内容中. 长尾关键词搜索量虽然非常少,而且不稳定.但是搜索量甚至超越热门目标关 ...
- phoenix中添加二级索引
Phoenix创建Hbase二级索引 官方文档 1. 配置Hbase支持Phoenix创建二级索引 1. 添加如下配置到Hbase的Hregionserver节点的hbase-site.xml ...
- 【PAT甲级】Public Bike Management 题解
题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourist ...
- 洛谷 P4124 (数位 DP)
### 洛谷 P4124 题目链接 ### 题目大意: 给你一段区间,让你求满足下列两个条件时的数的个数. 1.至少有 3 个相邻相同数字 (即 111 .1111 .222 等) 2.不能同时出现 ...
- [LeetCode#184]Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- 使用 Powershell 远程连接 windows server
使用 Powershell 远程连接 windows server Intro 最近我们的开发环境增加了一个 windows 服务器,没有界面的,不能直接远程桌面连上去管理,需要使用 Powershe ...
- 简析 Golang IO 包
简析 Golang IO 包 io 包提供了 I/O 原语(primitives)的基本接口.io 包中定义了四个最基本接口 Reader.Writer.Closer.Seeker 用于表示二进制流的 ...
- .net core程序强制以管理员权限启动
当我们编写windows程序的时候,很多时候需要程序默认以管理员权限运行,以前在.net 程序中直接新建一个app.manifest,设置requestedExecutionLevel 节点即可 &l ...
- Hive_hdfs导入csv文件
转自:Hive_hdfs csv导入hive demo 1 create csv file.student.csv 4,Rose,M,78,77,76 5,Mike,F,99,98,98 2 pu ...