【数组】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?

Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
思路:
这道题看上去有点摸不到头脑,其实细想,到达某一点路径数就等于到达它上一点和左边点的路径数之和。这样,我们就可以建立一个二维数组,进行求解即可。
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
var f=[];
for(var i=0;i<m;i++){
f[i]=[];
} for(var i=0;i<n;i++){
f[0][i]=1;
} for(var i=0;i<m;i++){
f[i][0]=1;
} for(var i=1;i<m;i++){
for(var j=1;j<n;j++){
f[i][j]=f[i-1][j]+f[i][j-1];
}
}
return f[m-1][n-1];
};
【数组】Unique Paths的更多相关文章
- 【数组】Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 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 ...
- 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 ...
- 62. Unique Paths
题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...
- 【LeetCode练习题】Unique Paths
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- Leetcode 动态规划 Unique Paths
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...
随机推荐
- Swift实现UIKit Dynamic动画
iOS7引入了UIKit Dynamics,可以帮助开发者开发出更接近真实世界的动画效果.之前,用户如果要做出这样的效果,需要话很多的时间在物理计算和Core Animation上.现在,所有的一切都 ...
- 怎样运用servlet
制作登陆界面 login.html <!DOCTYPE html> <html> <head> <title>login.html</title& ...
- 好用的下拉第三方——nicespinner
1.简介 GitHub地址:https://github.com/arcadefire/nice-spinner Gradle中添加: allprojects { repositories { ... ...
- logback 热修改
<configuration scan="true" scanPeriod="60 seconds" debug="false"> ...
- 关于fastjson的一个坑:输出json时,bean对象属性首字母默认被小写
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴. 主要特点: 快速FAST: 比其它任何基于Java的解析器和生成器更快,包括jackson 强大:支 ...
- 设置 ssh 使用public key 免密码登录
第一步,生成自己公钥, 私钥 1: ssh-keygen -t rsa 2: 3: root@yjlml:~# ssh-keygen -t rsa 4: Generating public/pri ...
- 在Visual Studio 2013中修改远程Git服务器的地址
在Visual Studio 2013中克隆了远程Git服务器的代码后,可以通过下图的方式修改Git服务器的地址:
- centos 安装vmware 9.02 报 Failed to load module "pk-gtk-module" "canberra-gtk-module"
http://www.linuxidc.com/Linux/2012-01/50944.htm 系统平台:RHEL6.1 X86 32bit 软件版本:VMware-Workstation-Full- ...
- asp.net MVC设计模式中使用iTextSharp实现html字符串生成PDF文件
因个人需求,需要将html格式转换成PDF并加上水印图片.于是乎第一次接触这种需求的小菜鸟博主我,在某度搜索引擎上不断的查阅关键字资料.踩坑,终于有了一个相应的解决方案.以下是解决步骤,记录下来方便以 ...
- ASP.NET WebAPI 测试文档 (Swagger)
ASP.NET WebAPI使用Swagger生成测试文档 SwaggerUI是一个简单的Restful API测试和文档工具.简单.漂亮.易用(官方demo).通过读取JSON配置显示API .项目 ...