【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 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?
意思很明确,只能往下往右,从左上角到右下角,有多少条路?
第一个思路:动态规划、
到达某一格的路径数目=到达它上一格的路径数目+达到它左侧一格的路径数目
由于只能往下或者往右,故第一行和第一列的所有格子都只有一条路径。
class Solution:
# @return an integer
def uniquePaths(self, m, n):
if m == 1 and n == 1:
return 1
feat = [[1] * n for row in range(m)]
feat[0][0] = 0
for i in range(1,n): #第一行第一列都是1
for j in range(1,m):
feat[j][i] = feat[j-1][i] + feat[j][i-1]
return feat[m-1][n-1]
第二个思路:用0,1分别表示往下,往右,对于m*n的格子,结果就是有多少种m-1个0和n-1个1的排列方式。
【leetcode】Unique Paths的更多相关文章
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【题解】【排列组合】【素数】【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 II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【Leetcode】【Medium】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 Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【数组】Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【数组】Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- js 阻止浏览器默认行为
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 转js中this指向的简明解答
JS中的this对象详解 JS中this关键字很常见,但是它似乎变幻莫测,让人抓狂.这篇文章就来揭示其中的奥秘. 借助阮一峰老师的话:它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. ...
- Linux centOS下搭建RTMP服务器的具体步骤
以下的所需的安装包,可直接在linux系统终端下载,也可从其他地方下载之后拷到对应目录下解压使用,遇到连接不到国外网站时可改变压缩包地址 1.安装依赖包: #yum install glibc.i68 ...
- js替换选中的文字
替换html中选中的文字 function replaceSelection() { if (window.getSelection) { var selecter = window.getSelec ...
- Regular Expression Matching leetcode
递归方法运行时间过长.考虑使用动态规划的方法. 代码如下: bool isMatch(string s, string p) { int i,j; int m=s.size(); int n=p.si ...
- NC 解决启动环境报内存溢出问题
java heap space 内存溢出 解决方法如下: 在eclipse中,window-->preferences-->Java-->Installed JREs选中JRE 点击 ...
- mysql主从切换
mysql 主从切换 主停,从做主步骤如下: 1 确认从服务器已经完成所有同步操作:stop slave io_thread show processlist 直到看到状态都为:xxx has rea ...
- maven 三个基本插件 clean dependency compiler
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Android数据库加密之sqlciher方案
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/6241964.html 前言 大家好,我是Cavalier ...
- kernel 对浮点的支持
http://blog.chinaunix.net/uid-22545494-id-316735.html 作者: Sam(甄峰) sam_code@hotmail.com 一:早期ARM上的浮点模 ...