LeetCode——Unique Paths
Question
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?
Note: m and n will be at most 100.
Solution 1
动态规划,到(i, j)的所有方法等于到(i - 1, j)加上(i, j - 1)。
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > table(m, vector<int> (n, 1));
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
table[i][j] = table[i - 1][j] + table[i][j - 1];
}
}
return table[m - 1][n - 1];
}
};
Solution 2
组合数学中的格路问题,注意要边乘边除,不然要越界。
class Solution {
public:
int uniquePaths(int m, int n) {
int N = n + m - 2;// how much steps we need to do
int k = m - 1; // number of steps that need to go down
double res = 1;
// 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 = 1; i <= k; i++)
res = res * (N - k + i) / i;
return (int)res;
}
};
LeetCode——Unique Paths的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] 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: Unique Paths I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- Leetcode Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- Cookies and Caching Client Identification
w HTTP The Definitive Guide 11.6.9 Cookies and Caching You have to be careful when caching documents ...
- Spark Streaming源码分析 – JobScheduler
先给出一个job从被generate到被执行的整个过程在JobGenerator中,需要定时的发起GenerateJobs事件,而每个job其实就是针对DStream中的一个RDD,发起一个Spark ...
- 【转】UML中类与类之间的5种关系表示
一.继承关系 继承指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功能的能力.在Java中继承关系通过关键字extends明确标识,在设计时一 ...
- python学习笔记(四)— 补充
函数return多个值 函数如果有多个return值,那么会生成一个元组里面 def hello(a,b,c,d): return a,b,c,d res =hello('aa','cc','dd', ...
- PHP 创建中文目录的情况
因为一个作业需要创建一些中文的目录,其实主要还是考虑一下编码问题. 首先确认下系统环境是什么编码,如果是gbk或者GB2312那就需要转下码,还有些特殊字符,就需要有个特殊的写法. iconv('UT ...
- 在HTML里面HEAD部分的META元素要表达的内容是什么
1.name属性主要有以下几种参数: A.Keywords(关键字) 说明:keywords用来告诉搜索引擎你网页的关键字是什么. 举例:<meta name ="keywords&q ...
- python序列化和json
python 的序列化和反序列化用于内存之间的共享,包括服务器和客户端的共享,两个Python程序之间的共享,以及以字符串的形式存储到硬盘中. pyhton 的pickle 可以对Python的各种数 ...
- LInux中的文件系统1
2017-03-08 10:37:55 一.虚拟文件系统VFS 文件系统用于将位于磁盘上的文件按照某种方式组织进内存,并给上层应用程序提供统一的访问接口.Linux支持多种文件系统EXT2/3,NTF ...
- error: Error: No resource found for attribute ‘layout_scrollFlags’ in package‘包名’
遇到error: Error: No resource found for attribute 'layout_scrollFlags' in package'包名' 这个问题时候刚開始自己也是感觉到 ...
- 通俗了解IaaS,PaaS,SaaS,看这里就对了(转)
[IT168 评论]云服务已经被大众所熟知,但对于刚接触云计算的朋友来说,仍然是云里雾里的绕着,今天小编就为你解读一下云计算的几种服务模式,IaaS,PaaS,SaaS到底是什么…区别有哪些? 字正腔 ...