Unique Paths
2015-06-14 10:17
489 查看
Description:
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://images0.cnblogs.com/blog2015/686769/201506/141012529414936.png)
Code:
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://images0.cnblogs.com/blog2015/686769/201506/141012529414936.png)
Code:
int uniquePaths(int m, int n) { //空间复杂度:O(MN) assert(m<=100 && n<=100); int path[MAX+1][MAX+1] = {0};//有效数字下标从1开始,便于阅读,因此这里加1 for (int i = 1; i <= m; ++i) { path[i][1] = 1; } for (int i = 1; i <= n; ++i) { path[1][i] = 1; } for (int i = 2; i <= m; ++i) {//因为m或n为1时的路径数都为1,所以不用再考虑 for (int j = 2; j <= n; ++j) { path[i][j] = path[i-1][j] + path[i][j-1]; } } return path[m] ; }
相关文章推荐
- looper、handler、messagequeue 总结
- Distinct Subsequences
- JDeveloper 配置 No suitable driver
- [ocUI日记]UIImage和UIImageview
- IOS UIKit 框架类之视图动画和坐标系介绍
- 【Android UI设计】Dialog对话框详解(一)
- iOS9 新增 UIStackView 官方文档翻译
- CS193p Lecture 11 - UITableView, iPad
- CS193p Lecture 10 - Multithreating, UIScrollView
- 基于链表实现Java 自定义Queue队列
- Android 之 Looper、MessageQueue、Handler 与消息循环
- java UID 问题
- VirtrueBox 镜像压缩
- [Algorithms] Longest Common Subsequence
- 理解MiniGUI消息循环和窗口过程
- Using JavaFX UI Controls 18 超链接
- 使用HMSegmentedControl实现分段控件
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
- UVA 11235 - Frequent values
- 【leetcode】Unique Binary Search Trees II