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 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

题目大意:

从m乘n矩阵左上角走到右下角,每一步只能向右或向下,求共有多少条不同路径。

递归解决:

 class Solution {
public:
vector<vector<int>> v; int solve(int m, int n) {//从(m, n)到(1, 1)有多少条路径
if (m == || n == )
return ;
if (v[m][n] >= )
return v[m][n];
v[m][n] = solve(m - , n) + solve(m, n - );
return v[m][n];
} int uniquePaths(int m, int n) {
if (m == || n == )
return ;
if (m == || n == )
return ;
v.resize(m + , vector<int>(n + , -));
return solve(m - , n) + solve(m, n - );
}
};

迭代:

 class Solution {
public: int uniquePaths(int m, int n) {
if (m == || n == )
return ;
vector<vector<int>> v(m, vector<int>(n));
int i, j;
for (i = ; i < m; i++) {
for (j = ; j < n; j++) {
if (i == || j == )
v[i][j] = ;
else
v[i][j] = v[i - ][j] + v[i][j - ];
}
}
return v[m - ][n - ];
}
};

最新文章

  1. ios-NSMutableAttributedString 更改文本字符串颜色、大小
  2. IOS笔记之UIKit_UIScrollView
  3. Java 四舍五入
  4. Indian Scientists Design Device to Collect Solar Energy 印度科学家设计太阳能收集设备
  5. UVa 10562 (特殊的输入处理方式) Undraw the Trees
  6. A Swift Tour(3) - Functions and Closures
  7. 题目:[NOIP1999]拦截导弹(最长非递增子序列DP) O(n^2)和O(n*log(n))的两种做法
  8. Makefile学习(二)[第二版]
  9. 详解用em替换px
  10. .Net Core微服务系列--开篇
  11. arcgis for js 之 获取两点之间的距离
  12. Tomcat启动分析(转自:http://docs.huihoo.com/apache/tomcat/heavyz/01-startup.html)
  13. 关于python的字符编码
  14. 【delphi】ClientDataSet详细解读
  15. 【转】JavaScript 事件顺序:冒泡和捕获
  16. 虚拟机上的Linux Java开发环境部署记录(VirtualBox+Ubuntu)第一章-基础环境搭建
  17. mongo数据集合属性中存在点号(.)
  18. 20145207《Java程序设计》实验一(Java开发环境的熟悉)实验报告
  19. web.xml中Filter的作用
  20. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

热门文章

  1. Unix或Linux中&amp;、jobs、fg、bg等命令的使用方法
  2. 基于原生态Hadoop2.6 HA集群环境的搭建
  3. django设置打印数据库日志
  4. Ant利用第三方的task
  5. java如何实现python的urllib.quote(str,safe=&#39;/&#39;)
  6. 深入理解JavaScript系列(16):闭包(Closures)
  7. UML建模—EA创建Class(类图)
  8. 5、栅格布局:ion-grid
  9. CssClass初步语法了解
  10. linux防火墙与端口设置