题目

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.

题解

其实跟爬梯子挺类似的,按个就是只能往上爬,这个就是方向可以换了下。同样想法动态规划。

分析方法也一样的,想想要到最右下角。到达右下角的方法只有两个,从上面往下,和从右面往左。

利用到达终点的唯一性,就可以写出递推公式(dp[i][j]表示到坐标(i,j)的走法数量):

dp[i][j] = dp[i-1][j] + dp[i][j-1]

初始条件的话,当整个格子只有一行,那么到每个格子走法只有1种;只有一列的情况同理。

所以,理解的这些,代码就非常好写了。

通常来讲,我们会初始dp数组为dp[m+1][n+1]。但是这里的话,因为dp[i][j]是表示坐标点,所以这里声明dp[m][n]更容易理解。

代码如下:

 1 public static int uniquePaths(int m, int n){  
 2              if(m==0 || n==0) return 0;  
 3              if(m ==1 || n==1) return 1;  
 4               
 5             int[][] dp = new int[m][n];  
 6               
 7             //只有一行时,到终点每个格子只有一种走法  
 8             for (int i=0; i<n; i++)  
 9                 dp[0][i] = 1;  
               
             // 只有一列时,到终点每个格子只有一种走法
             for (int i=0; i<m; i++)  
                 dp[i][0] = 1;  
               
             // for each body node, number of path = paths from top + paths from left  
             for (int i=1; i<m; i++){  
                 for (int j=1; j<n; j++){  
                     dp[i][j] = dp[i-1][j] + dp[i][j-1];  
                 }  
             }  
             return dp[m-1][n-1];  
         }  

Unique Paths leetcode java的更多相关文章

  1. Unique Paths [LeetCode]

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

  2. Unique Paths ——LeetCode

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

  3. 114. Unique Paths [by Java]

    Description A robot is located at the top-left corner of a m x n grid. The robot can only move eithe ...

  4. 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). ...

  5. Java for LeetCode 063 Unique Paths II

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

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

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

  8. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. LeetCode第[62]题(Java):Unique Paths 及扩展

    题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...

随机推荐

  1. django-QueryDict 对象

    在 HttpRequest 对象中,属性 GET 和 POST 得到的都是 django.http.QueryDict 所创建的实例.这是一个 django 自定义的类似字典的类,用来处理同一个键带多 ...

  2. 面向对象设计原则 里氏替换原则(Liskov Substitution Principle)

    里氏替换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一. 里氏替换原则中说,任何基类可以出现的地方,子类一定可以出现. LSP是继承复用的基石,只 ...

  3. 20172302『Java程序设计』课程 结对编程练习_四则运算第二周阶段总结

    一.结对对象 姓名:周亚杰 学号:20172302 担任角色:驾驶员(周亚杰) 伙伴第二周博客地址 二.本周内容 (一)继续编写上周未完成代码 1.本周继续编写代码,使代码支持分数类计算 2.相关过程 ...

  4. HNOI 越狱

    题目描述 监狱有连续编号为 1…N的 N 个房间,每个房间关押一个犯人,有 M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. 输入输出格式 ...

  5. android防止按钮连续点击方案之AOP

    转载请标明出处http://www.cnblogs.com/yxx123/p/6675567.html 防止连续点击的实现方式有很多种,比如,在所有的onclick里面加上防多次点击的代码,或者定义一 ...

  6. native与ascii互转

    package org.edujks.schoolapp.schoolbasedresearch.schoolsubject.action; public class DecodeUtil { /** ...

  7. .net中实现RSS方法

    引用 如何在.net动态网站中实现RSS呢?主要思想是编写一个能够自动按照RSS格式生成xml文档的通用类.具体步骤如下: 步骤一:创建RSS通用类 C#代码 using System;   usin ...

  8. 在阿里云里申请免费Https证书SSL

    在阿里云控制台:安全(云盾)->证书服务->购买证书里(地址:https://common-buy.aliyun.com/?spm=5176.2020520163.cas.1.zTLyhO ...

  9. delphi CreateAnonymousThread 匿名线程

    引用  http://www.cnblogs.com/del/archive/2011/05/18/2049913.html 先看一个非多线程的例子, 代码执行时不能进行其它操作(譬如拖动窗体): { ...

  10. 通过进程ID获取基地址

    下面代码是通过进程ID来获取进程的基地址,创建一个进程快照后,读取进程模块,一般情况下第一个模块就是进程的基地址,下面的程序通过模块的字符串匹配来找到基地址.通过MODULEENTRY32来读取,下面 ...