[DP] Unique Paths
2015-12-10 14:51
113 查看
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?
题意分析:要求计算从Start到Finish的所有可能路径。
分析:机器人只能向右活向下走。
m * n矩阵对应数组dp[m]
,数组中的每个值对应机器人走到该处所有可能的路径。
矩阵中的单元供分为两类:
像A1(对应第二维索引为0)和A2(对应第一维索引为0)这种,机器人走到该点只存在1种路径;
像B这种,到达B的所有路径为到达B1的路径加上到达B2的路径之和,即dp[i][j] = dp[i-1][j] + dp[i][j-1],
源码如下:
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?
题意分析:要求计算从Start到Finish的所有可能路径。
分析:机器人只能向右活向下走。
m * n矩阵对应数组dp[m]
,数组中的每个值对应机器人走到该处所有可能的路径。
矩阵中的单元供分为两类:
像A1(对应第二维索引为0)和A2(对应第一维索引为0)这种,机器人走到该点只存在1种路径;
像B这种,到达B的所有路径为到达B1的路径加上到达B2的路径之和,即dp[i][j] = dp[i-1][j] + dp[i][j-1],
源码如下:
public class Solution { public int uniquePaths(int m, int n) { if (m < 1 || n < 1) { return 0; } int[][] dp = new int[m] ; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i == 0 || j == 0) { dp[i][j] = 1; } else { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } } return dp[m - 1][n - 1]; } }
相关文章推荐
- Confluence5.8.10安装和破解
- Confluence5.8.10安装、破解、设置
- UIView提供坐标系转换方法
- IOS --- UITableView 表示图(头部和尾部)
- iOS中UITextField的字数限制
- (java)Implement Stack using Queues
- jfinal框架ueditor上传图片的实现方法
- windows下 BurpSuite pro安装
- iOS 代码实现获得应用的版本号(Version/Build)
- @GeneratedValue
- IOS-UIScrollView
- UI视图控件、视图嵌套、SubView、Tag 的使用
- UI - 绘制 - 文字(或者其他控件)阴影效果
- Lesson 26 Wanted: alarge biscuit tin
- 关于Socket的一些Guide1
- Parquet表在spark与Impala间兼容性测试
- Android blueZ HCI(二):hcitool hcidump常用方法
- request的一些方法
- Android blueZ HCI(一):hciconfig实现及常用方法
- Android bluetooth介绍(四): a2dp connect流程分析