Unique Paths 解答
2015-09-20 02:41
337 查看
Question
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?
Solution
The key to the solution is to create 2D array to record ways.public class Solution { public int uniquePaths(int m, int n) { int[][] dp = new int[m] ; dp[0][0] = 0; for (int i = 0; i < m; i++) dp[i][0] = 1; for (int i = 0; i < n; i++) dp[0][i] = 1; 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]; } }
相关文章推荐
- hdoj 1047 Integer Inquiry(几个大数连续加)
- Permutation Sequence (略)
- QuickCalc | 快计算
- CryptoGUI | 文件加解密
- UGUI 锚点坑
- ios(总结)UISearchBar
- 点击UICollectionViewCell上的UIButton如何获取当前的UICollectionViewCell?
- 【Xamarin 开发 IOS --使用 Storyboard Segue 实作 UIViewController 的切换 (实例)】
- 【Xamarin 开发 IOS --IOS 页面导航概念Segue】
- que0que.cc:: Short introduction to query graphs
- java GUI--day22
- Guide to installing 3rd party JARs
- 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)
- 使用存取方法来设置Property value
- iOS ui设计的乐土
- 初试jQuey
- UI控件——UISlider:滑动条
- iOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)
- easyui datagrid学习笔记
- UIButton超出UIView边界不可点击,应该怎么办?