【LeetCode】062. 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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
题解:
Solution 1 ()
class Solution {
public:
int uniquePaths(int m, int n) {
if(m< || n<) return ;
if(m == && n == ) return ;
vector<vector<int>> dp(m, vector<int> (n,));
for(int i=; i<m; ++i) {
for(int j=; j<n; ++j) {
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};
用一维数组存储,d[j] = d[j] + d[j-1];这里的等号右边d[j]相当于d[i-1][j],d[j-1]相当于d[i][j-1];
Solution 2 ()
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> dp(n, );
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
dp[j] += dp[j - ];
}
}
return dp[n - ];
}
};
First of all you should understand that we need to do n + m - 2 movements : m - 1 down, n - 1 right, because we start from cell (1, 1).
Secondly, the path it is the sequence of movements( go down / go right),
therefore we can say that two paths are different
when there is i-th (1 .. m + n - 2) movement in path1 differ i-th movement in path2.
So, how we can build paths.
Let's choose (n - 1) movements(number of steps to the right) from (m + n - 2),
and rest (m - 1) is (number of steps down).
I think now it is obvious that count of different paths are all combinations (n - 1) movements from (m + n-2). (from here)
Solution 3 ()
class Solution {
public:
int uniquePaths(int m, int n) {
int N = n + m - ;// how much steps we need to do
int k = m - ; // number of steps that need to go down
double res = ;
// here we calculate the total possible path number
// Combination(N, k) = n! / (k!(n - k)!)
// reduce the numerator and denominator and get
// C = ( (n - k + 1) * (n - k + 2) * ... * n ) / k!
for (int i = ; i <= k; i++)
res = res * (N - k + i) / i;
return (int)res;
}
};
【LeetCode】062. Unique Paths的更多相关文章
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】62. 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】063. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】980. Unique Paths III
题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square. Ther ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
随机推荐
- php程序执行过程--非宏观和微观而是写的程序一行一行的路径----利用xdebug了解一段程序的执行过程----覆盖率
1.xdebug_start_code_coverage();//在需要开始跟踪程序执行路径时使用 2.var_dump(xdebug_get_code_coverage());//在结尾使用打印程序 ...
- 安装部署zookeeper集群
实验说明: 三台虚拟机做zookeeper集群,集群个数最好是奇数个,原理详见zookeeper 详解 安装zookeeper 请确保jdk 已安装好,否则无法启动 三台虚拟机IP分别为:192. ...
- Java各类格式转换
1.Java 根据固定格式的Excel生成实体类 2.GPS 经纬度转换为 经过旋转后的平面坐标
- 【虚拟机】WIN8.1系统安装虚拟机win7环境
一.虚拟机的安装 1.准备 VMware Workstation 的软硬件支持,请查看 http://www.vmware.com/cn/products/workstation.html#techs ...
- PowerBuilder -- Len(), LenA() 与 String, Blob
使用的是Powerbuilder12.5与Powerbuild9 不太一样 函数 String Blob Len() 返回字符数 返回字符数对应的字节数 LenA() 返回字节数 返回字符数对应的字节 ...
- rpm包查看和解压(转)
From:http://www.51testing.com/html/57/28557-205195.html 查看rpm包内容: rpm -qpl *.rpm 解压rpm包: rpm2cpio *. ...
- KEIL下分散加载文件的使用(zt)
KEIL下分散加载文件的使用 对于分散加载的概念,在<ARM体系结构与编程>书中第11章有明确介绍. 分散加载文件(即scatter file 后缀为.scf)是一个文本文件,通过编写 ...
- scikit-learn:4. 数据集预处理(clean数据、reduce降维、expand增维、generate特征提取)
本文參考:http://scikit-learn.org/stable/data_transforms.html 本篇主要讲数据预处理,包含四部分: 数据清洗.数据降维(PCA类).数据增维(Kern ...
- python 基础 8.0 regex 正则表达式--常用的正则表达式
一. python 中常用的正则表达式 二. 正则表达式的网站,可以进行在线正则匹配 https://regex101.com/ 1. 使用方法及正则介绍 1> ‘.’ 匹 ...
- gulp的使用方法
---恢复内容开始--- 什么是gulp? Gulp.js是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务. 使用步骤: 1.全局安装gulp: npm install - ...