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?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
SOLUTION 1:
使用DP解决之。很简单。某一个cell有2种可能到达,从上面来,以及从左边来,只需要把每一个cell的可能数计算好,并且把它们相加即可。
另外,在第一行,第一列,以及左上角需要特别处理,因为我们上面、左边并没有cells.
public class Solution {
public int uniquePaths(int m, int n) {
if (m <= || n <= ) {
return ;
} int[][] D = new int[m][n]; for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (i == && j == ) {
D[i][j] = ;
} else if (i == ) {
D[i][j] = D[i][j - ];
} else if (j == ) {
D[i][j] = D[i - ][j];
} else {
D[i][j] = D[i - ][j] + D[i][j - ];
}
}
} return D[m - ][n - ];
}
}
LeetCode: Unique Paths 解题报告的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [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 ...
- LeetCode: Unique Paths I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
随机推荐
- mysq双主模式
准备环境:服务器操作系统为RHEL6.4 x86_64,为最小化安装.主机A和主机B均关闭防火墙和SELINUX ,IP地址分别为192.168.131.129和192.168.131.130,MyS ...
- OGLplus 0.33.0 发布,OpenGL 的 C 封装库
OGLplus 0.33.0 引入很多新的 OGLplus 和 OALplus 示例,更新了构建系统.CamMatrix::LookingAt 构造器.Texture::MaxLevel getter ...
- 【Leetcode】【Hard】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- NUnit-Console 命令行选项详解
本文为 Dennis Gao 原创或翻译技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. NUnit-Console 命令行选项 NUnit-Console 命令行选项列表 指定运行哪 ...
- jenkins2 javahelloworld
文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd 本文使用jenkins自动构建基于maven的 ...
- Microsoft Azure开发体验 – 网络报名系统
去年底RP好抢到了中国版Azure的使用机会,最近社团里讨论到9月份招新的问题,就用Azure Website和Azure Table Storage打造了这个报名系统. 网站放在 http://jo ...
- 易出错的C语言题目之一:宏定义与预处理
1.写出下列代码的运行结果: #include<stdio.h> #include<string.h> #define STRCPY(a,b) strcpy(a##_p,#b) ...
- duilib进阶教程 -- 扩展duilib的消息 (11)
duilib并没有提供双击和右键消息,所以需要我们自行扩展,这里以添加双击消息为例, 在UIDefine.h里,我们只看到了DUI_MSGTYPE_CLICK消息,却没有看到双击消息,因此需要在这里添 ...
- paip. java的 函数式编程 大法
paip. java的 函数式编程 大法 Java 语言中常被忽视的一个方面是它被归类为一种命令式(imperative)编程语言.命令式编程虽然由于与 Java 语言的关联而相当普及,但是并不是惟一 ...
- 360[警告]跨站脚本攻击漏洞/java web利用Filter防止XSS/Spring MVC防止XSS攻击
就以这张图片作为开篇和问题引入吧 <options>问题解决办法请参考上一篇 如何获取360站长邀请码,360网站安全站长邀请码 首先360能够提供一个这样平台去检测还是不错的.但是当体检 ...