【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). ...
随机推荐
- oracle java SE
http://www.oracle.com/technetwork/java/javase/downloads/index.html
- 用SQLMAP工具进行SQL注入
1.检查注入点 [注入点需要自己寻找,可以利用一些工具,例如:Acunetix Web Vulnerability scanner (WVS),AppScan等]u表示URL. sqlmap - ...
- CSS浮动布局与菜单栏设计
公司周六停电,终于可以双休了.用周五空余时间再夯实一下css基础,<CSS权威指南>概念性的内容看起来容易犯困,不如实践来得快,动手操作吧. 一.浮动布局 浮动存在问题:浮动使元素脱离文档 ...
- Ueditor 编译发布后无法使用上传图片、附件等功能
Ueditor 发布后上传到服务器会出现无法使用上传功能,在本地源代码模式下上传功能正常,这是因为在网站发布期间把 net/Uploader.cs 给编译了,发布后的代码不包含Uploader.cs故 ...
- iOStextFiled判断输入长度
个人在开发当中发现在用textField的代理方法 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(N ...
- 用python画xy散点图
import matplotlib.pyplot as plt plt.plot([1,2,3],[4,5,6],'ro') plt.show()#这个智障的编辑器 这样的话,就可以画一个散点图,图中 ...
- js跨域访问
什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: 首 ...
- HTTP协议-----小白
HTTP是一个属于应用层的面向对象的协议. ***OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 HTTP协议的主要特点可概括如下: ...
- jquery选择器总结 转自(永远的麦子)
jQuery选择器总结 阅读目录 1, 基本选择器? 2, 层次选择器? 3, 过滤选择器? 4, 表单选择器? jQuery选择器共有四大类,分别为基本选择器,层次选择器,过滤选择器和表单选择器.下 ...
- 树型dp
树形dp主要有两种,比较重要的共同点就是要想全所有情况. [一] 第一种是简单的父子关系型,即动规只与一个节点和它的子节点有关. [例]codevs1380没有上司的舞会: 有个公司要举行一场晚会.为 ...