【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). ...
随机推荐
- Textrank算法介绍
先说一下自动文摘的方法.自动文摘(Automatic Summarization)的方法主要有两种:Extraction和Abstraction.其中Extraction是抽取式自动文摘方法,通过提取 ...
- eclipse的安装与配置
eclipse的英文名是日蚀,一直很喜欢这个名字. 1.安装很简单,直接下载eclipse包,免安装的.解压后找到其执行文件,如图所示.
- 使用jekyll在GitHub Pages上搭建个人博客【转】
网上有不少资源,但大多是“授人以鱼”,文中一步一步的告诉你怎么做,却没有解释为什么,以及他是如何知道的.他们默认着你知道种种专业名词的含义,默认着你掌握着特定技能.你折腾半天,查资料,看教程,一步步下 ...
- sublime插件 cssComb实现css自动排序及格式化
cssComb是一个实现css代码自动排序,当然顺便也实现了代码的格式化 安装: 首先需要打开sublime搜索安装csscomb插件(前提是已经安装了sublime的package control) ...
- 配置文件keepalived.conf详解
keepalived.conf 一个功能比较完整的keepalived 的配置文件,其配置文件keepalived.conf 可以包含三个文本块:全局定义块.VRRP 实例定义块及虚拟服务 ...
- android app上线后bug的处理
app上线后,后期维护显得尤为重要,今天给大家分享一下app上线后出现bug后的解决方法 1.继承Application类,重写onCreate方法 import java.io.File; impo ...
- 构建angular项目
1. 安装yo与gulp bower $ npm install -g yo $ npm install -g gulp bower 2. 快速创建 $ npm install -g gene ...
- yaf框架学习笔记
1.yaf框架支持简单的试图引擎,并且支持用户自定义视图引擎,比如smarty. 2.Yaf_Request_Http::getQuery ,Yaf_Request_Http::getQuery ( ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- Spring overview
引子 接触Java很多年了,各种framework,却从未系统的去了解过.最近突然想清楚一件事,就是当下的目标——Focus on Java-based RESTful WS & JS.而之于 ...