Unique path
2015-09-23 11:26
453 查看
题目:
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?
![](http://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
解法一:自己写的,比较诡异
解法二:九章算法,好理解
reference:http://www.jiuzhang.com/solutions/unique-paths/
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?
![](http://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
解法一:自己写的,比较诡异
public int uniquePaths(int m, int n) { int [][] grids = new int [m+1][n+1]; grids[m][n-1]=1; for (int i=m-1;i>=0;i--) { for (int j=n-1;j>=0;j--) { grids[i][j]=grids[i+1][j]+grids[i][j+1]; } } return grids[0][0]; }
解法二:九章算法,好理解
public class Solution { public int uniquePaths(int m, int n) { if (m == 0 || n == 0) { return 0; } int[][] sum = new int[m] ; for (int i = 0; i < m; i++) { sum[i][0] = 1; } for (int i = 0; i < n; i++) { sum[0][i] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1]; } } return sum[m - 1][n - 1]; } }
reference:http://www.jiuzhang.com/solutions/unique-paths/
相关文章推荐
- PHP中include require include_once require_once 的区别
- iOS学习笔记30-could not build module 'Foundation' (使用Zbar二维码操作时)
- ios5中UIViewController addChildViewController等新方法
- BlockingQueue
- 改写控件之《runOnUiThread实现变换的TextView》,与Handle不同的体验
- 改写控件之《runOnUiThread实现变换的TextView》,与Handle不同的体验
- media query 开发总结
- iOS SDK详解之UIEvent/UITouch
- iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序
- UILabel小结
- DelayQueue
- SQL Server: Get table primary key and Foreign Key using sql query
- SQL Server: Get table primary key and Foreign Key using sql query
- UEditor 如何进行二次开发
- 全自动条码扫描仪
- iOS 7.1 的 Safari 为 meta 标签新增 minimal-ui 属性,在网页加载时隐藏地址栏与导航栏
- HDU 5136 Yue Fei's Battle
- 如何在UIView中使用视图控制器
- Web UI⾃动化测试平台,专治Web 的各种疑难杂症。
- MySQL 存储4个字节 java.sql.SQLException: Incorrect string value: '\...' at row 1