题目

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. leetcode 两数之和 python

      两数之和     给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 1 ...

  2. 一个线上程序bug,由通用补数程序引起

    下游发现接口可用率非100%,马上线上查看,发现数据在有些情况下通用补数的数据是空, 有20%的用户是没有相应偏好等的数据的,需要通用补数来补数,结果通用补数没有数据. 通用补数数据的检查报警时必须要 ...

  3. 吴恩达-coursera-机器学习-week6

    十.应用机器学习的建议(Advice for Applying Machine Learning) 10.1 决定下一步做什么 10.2 评估一个假设 10.3 模型选择和交叉验证集 10.4 诊断偏 ...

  4. Xtreme8.0 - Sum it up 水题

    Sum it up 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/sum-it-up Descr ...

  5. Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学

    C. Pythagorean Triples 题目连接: http://www.codeforces.com/contest/707/problem/C Description Katya studi ...

  6. USBDM Kinetis Debugger and Programmer

    Introduction The FRM-xxxx boards from Freescale includes a minimal SWD based debugging interface for ...

  7. 《Go学习笔记 . 雨痕》流程控制(if、switch、for range、goto、continue、break)

    Go 精简(合并)了流控制语句,虽然某些时候不够便捷,但够用. if...else... 条件表达式值必须是布尔类型,可省略括号,且左花括号不能另起一行. func main() { x := 3 i ...

  8. 在单链表的第i个位置后插入一个节点(阿里+腾讯等面试题总结)

    时间:2014.04.26 地点:基地 ------------------------- 一.题目 题目是非常easy和基础,就是在单链表的第i个位置后插入一个节点.要求写代码,5分钟之内完毕.面腾 ...

  9. 让Less在Dreamweaver中高亮显示

    装的Dreamweavercs6还是没支持less啊,试过几种方法,感觉还是插件比较直接, 下载插件:http://download.csdn.net/download/lml_231/8556517 ...

  10. Node.js + Express 构建的订餐系统

    Node.js的版本 - v0.8.12 Express的版本 – v3.3.3  (安装 $ npm install -g express) 系统的登录逻辑是:获取用户名 + 密码,向内网RTX服务 ...