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.


题目标签:Array

  这道题目给了我们一个matrix 的m 和n, 让我们从[0,0] 开始,找到所有不同的路径到[m-1][n-1]。我们可以来看一个比原题简单一点的例子:

  3 * 3 matrix,我们设起点为1。每一个点代表的是,走到这个点的不同路径的数量

1  0  0

0  0  0

0  0  0

  我们可以发现,机器人只能往右和下走,所以路径的可能只能来自于左和上。

  所以我们可以遍历matrix,把这个点的左边点,和上面点加起来,就等于到这个点的路径之和。

  1  1  1

  1  2  3

  1  3  6

  最后一个点,[m-1][n-1]就是所有不同路径的总数。

 

Java Solution:

Runtime beats 5.14%

完成日期:07/20/2017

关键词:Array

关键点:Dynamic Programming, 逆向思考

 public class Solution
{
public int uniquePaths(int m, int n)
{
int[][] matrix = new int[m][n]; matrix[0][0] = 1; // start point for(int i=0; i<m; i++) // row
{
for(int j=0; j<n; j++) // column
{
// get left
if(j-1 >=0)
matrix[i][j] += matrix[i][j-1];
// get top
if(i-1 >=0)
matrix[i][j] += matrix[i-1][j];
}
} return matrix[m-1][n-1];
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4353555.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 62. Unique Paths(所有不同的路径)的更多相关文章

  1. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  3. [LeetCode] 62. Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. LeetCode 62. Unique Paths不同路径 (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  5. [leetcode]62. Unique Paths 不同路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  6. [LeetCode] 63. 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 ...

  7. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  8. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  9. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

随机推荐

  1. 读取properties属性文件

    1.通过类加载器加载 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Chap ...

  2. java如何将html过滤为纯文本

    java开发中jsp页面可以嵌套很多插件就可以将html形式的文本直接转化为纯文本,但是如果你已经保存下来或者没有运用插件,这个额html形式的文本你该怎么转化为纯文本呢?有次我将公告保存了html形 ...

  3. 记一次 node.js 的populate用法

    最近在学习node.js做一个简单的博客的网站,用的express框架和mongodb的数据库.之前没有接触过这个数据库,所有在一开始写的时候遇到了一些问题,如何初始化model类型,又如何实现简单的 ...

  4. Bootstrap对齐方式

    <p class="text-left">我居左</p> <p class="text-center">我居中</p& ...

  5. Java对象大小:size和retained size

    最近看到网上很多文章讲如何计算java对象的大小(size),很多观点不敢苟同. 这是其中一篇比较靠前的文章,写的也比较全面: http://blog.csdn.net/iter_zc/article ...

  6. ArrayList ConcurrentModificationException

    1.ConcurrentModificationException ConcurrentModificationException 出现在使用 ForEach遍历,迭代器遍历的同时,进行删除,增加出现 ...

  7. POJ-1273-Drainage Ditches(网络流之最大流)

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...

  8. Java 中的语法糖

    百度百科对语法糖的定义 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这 ...

  9. E. Fish (概率DP)

    E. Fish time limit per test 3 seconds memory limit per test 128 megabytes input standard input outpu ...

  10. Java历程-初学篇 Day07 循环结构2 for循环

    一,格式 for(赋值语句//为循环变量赋初值;条件语句//循环结构的循环条件;赋值语句//迭代,修改循环变量的值){ //循环体; } 二,简单举例 for(int i=1;i<=10;i++ ...