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.

Show Tags
 

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 解题报告的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】576. Out of Boundary Paths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. [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 ...

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

随机推荐

  1. stringbuffer与stringbuilder与String

    1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们 ...

  2. IIS Connection Timeout vs httpRuntime executionTimeout

    IIS Connection Timeout specifies how long, in seconds, should the code wait before timing out from t ...

  3. Python操作memcached及redis

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  4. 关于oralce字符集问题(复制别人的,纯属自己学习)

    本文主要讨论以下几个部分: 1.如何查看查询oracle字符集. 2.修改设置字符集以及常见的oracle utf8字符集 3.oracle exp 字符集问题 正文: 一.字符集参数 影响Oracl ...

  5. Twisted网络编程入门

    Twisted是用Python实现的基于事件驱动的网络引擎框架,功能非常丰富,基本包括了常用的网络组件. 所谓事件驱动,就是说程序就像是一个报警器(reactor),时刻等待着外部事件(event), ...

  6. C语言 数组 列优先 实现

    C语言数组结构列优先顺序存储的实现 (GCC编译). 从行优先转换为列优先存储方式, 与行优先相比, 不同之处在于改变了数组维界基址的先后顺序, 从而改变了映像函数常量基址. /** * @brief ...

  7. Android开源项目分包方式学习(eoe、oschina、github)

    总感觉Android中关于分包的文章很少,或者几乎可以说没有.但是合理地分包,又可以使整个项目模块化,减少包与包之间的依赖,让整个项目的框架更加清晰,更利于后续功能的拓展. 因为没有相关的文章,所以这 ...

  8. Redis学习笔记一:基本安装和配置

    1.安装 wget http://download.redis.io/releases/redis-3.2.3.tar.gz编译安装: tar xf redis-3.2.3.tar.gz cd red ...

  9. atitit.hbnt orm db 新新增更新最佳实践o99

    atitit.hbnt orm db 新新增更新最佳实践o99 1. merge跟个save了. 1 2. POJO对象处于游离态.持久态.托管态.使用merge()的情况. 1 3. @Dynami ...

  10. 文件上传小技巧/原生态【html篇】

    引语:大家都知道,html中上传文件就一个input,type=file就搞定了.但是,这个标签的样式,实在不值得提点什么,要改动他的样式,恐怕也是较难的.但是其实挺简单,今天就来说说上传文件小技巧吧 ...