题目

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.  1 public static int uniquePaths(int m, int n){  
  2.  2              if(m==0 || n==0) return 0;  
  3.  3              if(==1 || n==1) return 1;  
  4.  4               
  5.  5             int[][] dp = new int[m][n];  
  6.  6               
  7.  7             //只有一行时,到终点每个格子只有一种走法  
  8.  8             for (int i=0; i<n; i++)  
  9.  9                 dp[0][i] = 1;  
  10.                
  11.              // 只有一列时,到终点每个格子只有一种走法
  12.              for (int i=0; i<m; i++)  
  13.                  dp[i][0] = 1;  
  14.                
  15.              // for each body node, number of path = paths from top + paths from left  
  16.              for (int i=1; i<m; i++){  
  17.                  for (int j=1; j<n; j++){  
  18.                      dp[i][j] = dp[i-1][j] + dp[i][j-1];  
  19.                  }  
  20.              }  
  21.              return dp[m-1][n-1];  
  22.          }  

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. linux 输入设备驱动

    <输入子系统简介> a:背景 内核的输入子系统是对“分散的”,“多种不同类别”的输入设备(键盘,鼠标,跟踪杆,触摸屏,加速度计等)进行“统一处理”的驱动程序.具有如下特点: a-1:统一各 ...

  2. luogu P4779 【模板】单源最短路径(标准版)

    线段树优化dij 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 我可能是个智障 // luogu-judger-enable-o2 #pragma GCC diagnostic error "-std= ...

  3. code vs 2602 最短路径问题

    题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间.其中的一些点之间有连线.若有连线,则表示可从一个点到达另一个点,即两点间有通路,通 ...

  4. Atcoder Contest069F:Flag

    题目:https://arc069.contest.atcoder.jp/tasks/arc069_d 题意就是让你在n对数字每一对都选一个数使得任意两个数做差的绝对值最小值最大. 关系显然是一个2- ...

  5. [CC-FNCS]Chef and Churu

    [CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...

  6. 【BZOJ-4184 】 Shallot 线段树按时间分治 + 线性基

    4184: shallot Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 356  Solved: 180[Submit][Status][Discu ...

  7. Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题

    A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...

  8. IE中div被视频遮住的解决方法

    使用embed来内嵌视频,因为视频是windows media player,上面想用div浮动一些内容,之前尝试了一些方法,比如 1. 通过设定不同组件的z-index值 2. 通过设定 wmode ...

  9. Revit Family API 添加对齐

    没测试成功,留待以后研究. [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] ; ; i < nV ...

  10. Spring Boot开发之流水无情(二)

    http://my.oschina.net/u/1027043/blog/406558 上篇散仙写了一个很简单的入门级的Spring Boot的例子,没啥技术含量,不过,其实学任何东西只要找到第一个突 ...