题目来源:

  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. RedisService

    package com.sprucetec.bone.common.redis;import com.alibaba.fastjson.JSON;import org.springframework. ...

  2. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  3. HDU 2588 GCD

    题目大意:给定N,M, 求1<=X<=N 且gcd(X,N)>=M的个数. 题解:首先,我们求出数字N的约数,保存在约数表中,然后,对于大于等于M的约数p[i],求出Euler(n/ ...

  4. iOS6与iOS7屏幕适配技巧

    一.没有包装任何 导航控制器 或者 UITabBarController 1.控制器的view是UIScrollView\UITableView\UICollectionView时(控制器是UITab ...

  5. linux下安装python3.3.4

    下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...

  6. 基于Proxy思想的Android插件框架

    意义 研究插件框架的意义在于下面几点: 减小安装包的体积,通过网络选择性地进行插件下发 模块化升级.减小网络流量 静默升级,用户无感知情况下进行升级 解决低版本号机型方法数超限导致无法安装的问题 代码 ...

  7. IOS obj-c、c、c++混编

    今天发现这个问题,上网找了一下资料,发现原来如下: .m 文件可以混合c 和 objective-c 代码 .mm  文件可以混合 c c++ objective-c 代码 .c  .cpp  不能混 ...

  8. Webform中linq to sql多条件查询(小练习)

    多条件查询:逐条判断,从第一个条件开始判断,如果满足,取出放入集合,再从集合中查询第二个条件... aspx代码: <body> <form id="form1" ...

  9. hdu 1251 统计难题(trie 树的简单应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给你多个字符串,求以某个字符串为前缀的字符串数量. 思路:简单的trie数应用,在trie ...

  10. hive 分区操作记录

    创建分区: alter table table_name add partition (dt='20150423') location '/data/text/20150423';