[LeetCode]题解(python):062 Unique path
题目来源
https://leetcode.com/problems/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?
题意分析
Input:m、n
Output:the number of unique path
Conditions:输出所有可能的路径
题目思路
一看想起了全排列,注意到(m=3,n=7)时,表示要向下2步向右6步,那么全排列可得路径为f(2+6)/(f(2)*f(6)),f为阶乘函数
AC代码(Python)
__author__ = 'YE' class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def fac(n):
f = 1
for i in range(1, n + 1):
f *= i
return f m -= 1
n -= 1
if m == 0 or n == 0:
return 1
return fac(m + n) / (fac(m) * fac(n)) print(Solution().uniquePaths(3, 7))
[LeetCode]题解(python):062 Unique path的更多相关文章
- [Leetcode 62]机器人走路Unique Path 动态规划
[题目] A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
- 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...
随机推荐
- BZOJ 1001 & SPFA
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Description 现在小朋友们最喜欢的"喜羊羊与灰太狼 ...
- POJ 3281 (最大流+匹配+拆点)
题目链接:http://poj.org/problem?id=3281 题目大意:有一些牛,一堆食物,一堆饮料.一头牛要吃一份食物喝一份饮料才算满足,而且牛对某些食物和饮料才有好感,问最多有多少头牛是 ...
- 一份spring配置文件及其详解
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/axu20/archive/2009/10/14/4668188.aspx 1.基本配置:<?xml versio ...
- TYVJ P1093 验证数独 Label:none
背景 XX学校风靡一款智力游戏,也就是数独(九宫格),先给你一个数独,并需要你验证是否符合规则. 描述 具体规则如下:每一行都用到1,2,3,4,5,6,7,8,9,位置不限,每一列都用到1,2,3, ...
- 【BZOJ】1441: Min(裴蜀定理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1441 这东西竟然还有个名词叫裴蜀定理................ 裸题不说....<初等数 ...
- 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...
- 【wikioi】1296 营业额统计
题目链接:http://www.wikioi.com/problem/1296/ 算法:Splay 这是非常经典的一道题目,用Splay树来维护营业额,每天的最小波动值就等于 min{树根-树根的前驱 ...
- 怎样将文件夹打包为jar包或war包
在dos命令中,找到要打包文件夹位置,输入jar -cvf 打包后的名和格式 文件夹名 如图: 在我的文件夹的位置就能看到jar包和war包了
- git实用攻略
Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 下面是实用教程,免基础. 一.安装git https://git-scm.com/downloads 二.git创建 ...
- Linux邮件服务器架构
// 上面的过程只是实现了简单的本地用户的文件发送功能,只需要安装mailutil,不需要安装配置sendmail,看鸟哥的Linux私房菜中写的应该是本地用户发送邮件不需要sendmail.只有当非 ...