【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 ...
随机推荐
- Source-php-request-2
php比較坑的地方就是实现相同的目的,能够使用超级多种手段.比方(file_get_contents和fopen以及如今提到的curl以及fsockopen当然还有socket)这对于一个经验少的程序 ...
- jquery插件获取事件类型
//需要在使用函数时传入event关键字 $('[name=lprice]').change(function(event){ $('[name=lprice]').validate({ event: ...
- android之Context对各种服务的管理
经常,当我们须要用到服务的时候能够通果Context来获取:Context.getSystemService(name):比方:当我们想知道当前电话状态(来电/去电/sim卡状态等)时候,我们能够通过 ...
- 在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- Spark源码分析之五:Task调度(一)
在前四篇博文中,我们分析了Job提交运行总流程的第一阶段Stage划分与提交,它又被细化为三个分阶段: 1.Job的调度模型与运行反馈: 2.Stage划分: 3.Stage提交:对应TaskSet的 ...
- 4.锁--并行编程之条件变量(posix condition variables)
在整理Java LockSupport.park()的东东.看到了个"Spurious wakeup".又一次梳理下. 首先来个<UNIX环境高级编程>里的样例: [c ...
- WinDbg调试分析 net站点 CPU100%问题
WinDbg调试分析 asp.net站点 CPU100%问题 公司为了节省成本,最近有一批服务器降了配置,CPU从8核降到了2核.本身是小站点,访问量也不高,CPU总是会飙到100%而且可以一直持续几 ...
- KVM+VNC 虚拟机远程管理
1.安装kvm grep -E -o 'vmx|svm' /proc/cpuinfo #检查服务器是否支持虚拟化(vmx为interl平台.svm是AMD平台) #安装KVM所需软件包: yum gr ...
- 玩转 eclipse:[2]代码重构
Java 程序重构的目标就是进行全系统程序代码变更, 使得工程更符合常用设计思想,它不但不会影响程序的行为 ,反而使程序的结构更为清晰合理. Eclipse 提供一系列非常高效并且有易于重构程序代码的 ...
- 图像处理之基础---二维卷积c实现
http://wenku.baidu.com/link?url=4RzdmvP9sdaaUbnVEW4OyBD-g67wIOiJjKFF3Le_bu7hIiBS7I6hMcDmCXrQwsHvrsPv ...