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?

思路: 如果使用递归,那么DFS(i,j)=v[i][j]+max{DFS(i,j-1), DFS(i-1,j)},问题是DFS(i,j)可能会被计算两次,一次来自它右边的节点,一次来自它下面的节点。每个节点都被计算两次,时间复杂度就是指数级的了。解决方法是使用动态规划存储节点信息,避免重复计算。

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n]; dp[][] = ;
for(int i = ; i< n; i++ )
{
dp[][i] = ;
}
for(int i = ; i< m; i++ )
{
dp[i][] = ;
} for(int i = ; i< m; i++)
{
for(int j = ; j< n; j++)
{
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};

最新文章

  1. iOS 支持 IPv6
  2. 递推 hdu 2064
  3. apk反编译
  4. nginx负载均衡基于ip_hash的session粘帖
  5. linux-yum
  6. linux socket
  7. Java的静态导入
  8. jquery iframe自适应高度[转]
  9. 简单搭建React-Native环境
  10. Spring 下载与安装以及spring 3.2.9 jar包详解
  11. 搭建一个Web应用
  12. javascript Klass 实现
  13. POJ1942——Paths on a Grid(组合数学)
  14. Android M(6.0) 权限爬坑之旅
  15. 设计模式的征途—20.备忘录(Memento)模式
  16. 爬虫入门 手写一个Java爬虫
  17. week1 - Python基础1 介绍、基本语法、流程控制
  18. [转]Examining Open vSwitch Traffic Patterns
  19. 最小生成树Prim算法和Kruskal算法
  20. BugPhobia开发篇章:Beta阶段第IX次Scrum Meeting

热门文章

  1. java基础第6天
  2. L185 Ocean Shock
  3. c# sqlbulkcopy批量插入数据
  4. C# 调用C++ DLL 的类型转换(转载版)
  5. 归并排序(C语言)
  6. html模拟组织架构横向展开
  7. C#多线程应用:子线程更新主窗体控件的值(二)
  8. swing版网络爬虫-丑牛迷你采集器2.0
  9. 02 - Unit05:加载笔记列表
  10. [模板]ST表浅析