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的更多相关文章

  1. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  2. 【题解】【排列组合】【素数】【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 ...

  3. 【题解】【矩阵】【回溯】【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 ...

  4. 【Leetcode】【Medium】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. 【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 ...

  6. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. 【数组】Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. 【数组】Unique Paths

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

随机推荐

  1. 安装运行okvis

    1. 安装依赖项 sudo apt-get install cmake   //cmake sudo apt-get install libgoogle-glog-dev  // glog是Googl ...

  2. ElasticSearch 嵌套映射和过滤器及查询

    ElasticSearch - 嵌套映射和过滤器 Because nested objects are indexed as separate hidden documents, we can’t q ...

  3. Android开发学习---使用XmlPullParser解析xml文件

    Android中解析XML的方式主要有三种:sax,dom和pull关于其内容可参考:http://blog.csdn.net/liuhe688/article/details/6415593 本文将 ...

  4. Reverse链表 递归实现

    #include<iostream> struct node{ int payload; node* next; }; void bianli(node* head){ node* ite ...

  5. 压缩文本、字节或者文件的压缩辅助类-GZipHelper

    下面为大家介绍一.NET下辅助公共类GZipHelper,该工具类主要作用是对文本.字符.文件等进行压缩与解压.该类主要使用命名空间:System.IO.Compression下的GZipStream ...

  6. 有1,2,3一直到n的无序数组,排序

    题目:有1,2,3,..n 的无序整数数组,求排序算法.要求时间复杂度 O(n), 空间复杂度O(1). 分析:对于一般数组的排序显然 O(n) 是无法完成的. 既然题目这样要求,肯定原先的数组有一定 ...

  7. RancherOS Hyper-V 安装

    RancherOS Hyper-V 安装, Install to disk 打开 Hyper-V 管理界器, 新建虚拟机 输入名称和存储位置 选择一代 最低 1024M 配置网络 创建虚拟磁盘 设置启 ...

  8. 常用邮件服务器名(POP3,SMTP地址)

    POP3服务器地址:pop3.sina.com.cn(端口:110)SMTP服务器地址:smtp.sina.com.cn(端口:25) sina.cn: POP3服务器地址:pop3.sina.com ...

  9. Hadoop2.6.0的事件分类与实现

    前言 说实在的,在阅读Hadoop YARN的源码之前,我对于java枚举的使用相形见绌.YARN中实现的事件在可读性.可维护性.可扩展性方面的工作都值得借鉴. 概念 在具体分析源码之前,我们先看看Y ...

  10. using 语句中使用的类型必须可隐式转换为“System.IDisposable

    在使用 EF 出现 using 语句中使用的类型必须可隐式转换为“System.IDisposable 今天写在这里分享给大家 出现这样的问题,是因为没有引用  EntityFramework 这个程 ...