题目来源:

  https://leetcode.com/problems/unique-paths/


题意分析:

  给定两个整型m,n。判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走)。


题目思路:

  这可以看成一个组合问题。从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右。也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1)。


代码(python):

  

 import math
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
ans = 1;tmp = 1;m -= 1;n -= 1
t = min(m,n)
i = 0
while i < t:
ans *= (m + n - i)
tmp *= (t - i)
i += 1
return ans /tmp

转载请注明出处:http://www.cnblogs.com/chruny/p/5008210.html

[LeetCode]题解(python):062-Unique Paths的更多相关文章

  1. 【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). ...

  2. Java for 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 ...

  3. 【leetcode】62.63 Unique Paths

    62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...

  4. LeetCode(63)Unique Paths II

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

  5. LeetCode(62)Unique Paths

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

  6. 062 Unique Paths 不同路径

    机器人位于一个 m x n 网格的左上角, 在下图中标记为“Start” (开始).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角,在下图中标记为“Finish”(结束).问有多少条不 ...

  7. LeetCode题解之Binary Tree Paths

    1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root ...

  8. [LeetCode]题解(python):062 Unique path

    题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x  ...

  9. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  10. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

随机推荐

  1. HDU 4622 Reincarnation(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...

  2. vim之执行shell命令

    vim中执行shell命令,有以下几种形式 (1) :!command 不退出vim, 并执行shell命令command, 将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 (2)   ...

  3. Jquery遍历数组之$.inArray()方法介绍

    $.inArray()函数用于在数组中搜索指定的值,并返回其索引值.如果数组中不存在该值,则返回-1; $.inArray(value,array)    --value是要查找的值,array是被查 ...

  4. Android核心基础

    第三代移动通讯技术(3rd Generation) ,支持高速数据传输的蜂窝移动通讯技术.3G与2G的主要区别是传输数据的速度. 1987年,第一台模拟制式手机(1G)问世,只能进行语音通话,型号:摩 ...

  5. myeclipse 配置weblogic 异常

    java.lang.UnsupportedClassVersionError: Bad version number in .class file当前JDK与weblogic版本不匹配.

  6. windows server2008 r2修改远程桌面连接端口。

    1. windows 2008远程桌面端口默认是用的是3389端口,但是由于安全考虑,通常我们安装好系统后一般都会考虑把原来的3389端口更改为另外的端口.   2.更改过程: 2-1.打开注册表:  ...

  7. c#的Marshal

    补充过程中~ 感觉应该是C#调用非托管的比较专门的class 例1. public struct ImageDataMsg { public char DataType; public int Srv ...

  8. $.ajax和vue-resource实现OAuth

    Vue.js——使用$.ajax和vue-resource实现OAuth的注册.登录.注销和API调用 概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST A ...

  9. 转: Transact-sql游标使用详解~~很详细

    /*原理:游标就是把数据按照指定要求提取出相应的数据集,然后逐条进行数据处理.1.1游标的概念 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一 ...

  10. 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字

    题目:给定一个长度为N的数组,其中每个元素的取值范围都是1到N.判断数组中是否有重复的数字.(原数组不必保留) 方法1.对数组进行排序(快速,堆),然后比较相邻的元素是否相同.时间复杂度为O(nlog ...