题目

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. hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形

    How Many Triangles 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5784 Description Alice has n poin ...

  2. 整理c# 不常用但有用代码

    # 整理c# 不常用但有用代码 1.winform窗体右键菜单打开其他窗体 private void contextMenuStripHandler_Click(object sender, Even ...

  3. 允许mysql远程用户连接。

    默认mysql是禁止远程用户连接的.连接提示: 1045,“Access denied for user 'root'@'192.168.100.1' (using password:YES)&quo ...

  4. CAP原则(CAP定理)、BASE理论

    CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. CA ...

  5. spring-boot 速成(8) 集成druid+mybatis

    spring-boot与druid.mybatis集成(包括pageHelper分页插件), 要添加以下几个依赖项: compile('mysql:mysql-connector-java:6.0.5 ...

  6. 使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题

    在使用CEF的过程中,我发现了一个现象:WPF版的CEF比Chrome性能要差:一些有动画的地方会掉帧(例如,CSS动画,全屏图片拖动等),视频播放的效果也没有Chrome流畅. 查了一下相关资料,发 ...

  7. Extended APDU support

    http://pcsclite.alioth.debian.org/ccid_extended_apdu.html To be able to use an extended APDU you nee ...

  8. win10镜像重装,快速设置之后无限重启怎么办?

    我得操作过程: 1.下载老毛桃最win8pe装机版 2.安装老毛桃到U盘, 3.格式化U盘,用extFat格式,拷贝win10镜像文件(你也可以拷贝到硬盘上) 4.插入电脑,通过U盘启动,进入PE 5 ...

  9. Maven settings配置中的mirrorOf

    原文地址:http://blog.csdn.net/isea533/article/details/21560089 使用maven时,从来没仔细注意过setting配置节点的作用,直到今天配置总是不 ...

  10. cocos2d-x 清空缓存

    如场景切换  在内存吃紧的情况下 我们可以选择 先清理一下缓存 // 清空缓存 CCDirector::sharedDirector()->purgeCachedData();