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

Note: m and n will be at most 100.

思路1:

一上去就觉得是简单题:

如果当前格位于第m行或第n列,则只有一种路径;

否则当前格路径数等于“右格路径数”+“下格路径数”;

代码(未AC):

 class Solution {
public:
int uniquePaths(int m, int n) {
if (m == || n == )
return ; return uniquePaths(m - , n) + uniquePaths(m, n - );
}
};

结果提示超时了。

思路2:

考虑超时原因很可能是使用了函数递归,为避免使用递归,新建一个m*n的矩阵空间用于保存每个点计算的路径数。用新建空间保存结果,代替递归。

代码(AC):

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

思路3:

上述代码时间复杂度o(m*n),空间复杂度o(m*n),通过观察路径数量规律,还可以减少空间复杂度为o(n)。

已知grid[i][j] = grid[i-1][j] + grid[i][j-1];

进一步将后一项grid[i][j-1]替换为grid[i-1][j-1] + grid[i][j-2];

不断查分后一项,最终grid[i][j] = grid[i-1][j] + grid[i-1][j-1] + grid[i-1][j-2] + ... + grid[i-1][1] + grid[i][0];

又因为grid[i][0] = grid[i-1][0] = 1;

所以grid[i][j] 就等于第i-1行,从0到j所有元素之和;

得到了这个规律,我们只需要一个长度为n的数组col,通过第0行计算第1行,并不断迭代,最终得到第m行格子存在的路径数,此时col[n-1]即为所求.

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

思路4:

可以通过分析排列组合暴力求解:

从格子起始,一共需要移动n+m-2步,可以到达终点。

这n+m-2步中,有m-1步需要向下移动。

问题转化为,从n+m-2步中,选择m-1步向下移动,有多少种选择方法。

因此通过计算Combination(n+m-2, m-1)即可求得答案.

代码(超时):

 class Solution {
public:
int uniquePaths(int m, int n) {
long long dividend = ;
long long divisor = ;
for (int i = ; i <= m - ; ++i) {
dividend *= i + n - ;
divisor *= i;
} return int(dividend / divisor);
}
};

代码超时,未AC,正要放弃,看了讨论区的代码..原来用浮点数直接除,结果是正确的;

即(代码AC):

 class Solution {
public:
int uniquePaths(int m, int n) {
double res = ;
for (int i = ; i <= m - ; ++i) {
res = res * (i + n - ) / i;
} return int(res);
}
};

【Leetcode】【Medium】Unique Paths的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Unique Paths(唯一的路径数)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...

  5. 【leetcode刷题笔记】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. 【leetcode刷题笔记】Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  10. 【leetcode刷题笔记】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. n皇后问题(分析)

    这道题需要用到回溯算法,现在在这里先简单的介绍一下这个算法: 回溯算法也叫试探法,它是一种系统地搜索问题的解的方法.回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试.用回溯 ...

  2. android volley 发送 POST 请求

    Map<String, String> params = new HashMap<String, String>(); params.put("fromUser&qu ...

  3. oracle set命令详解

    SQL>set colsep '|'; //输出分隔符eg.SQL> set colsep '|';SQL> select * from dept; DEPTNO|DNAME     ...

  4. 存储器的保护(二)——《x86汇编语言:从实模式到保护模式》读书笔记19

    接着上一篇博文说. 5.代码段执行时的保护 每个代码段都有自己的段界限.同栈段一个道理,有效界限和G位相关. G=0:有效界限 = 描述符中的段界限 G=1:有效界限 = 描述符中的段界限值 * 0x ...

  5. springboot项目更改代码后实时刷新问题

    在spring boot使用的过程中, 发现我修改了静态文件, 前台刷新后, 没有任何变化, 必须重新启动, 才能看到, 这简直不能让人接受. 那有什么方法来解决这个问题呢. Baidu之后, 得到了 ...

  6. Vue父子组件生命周期执行顺序及钩子函数的个人理解

    先附一张官网上的vue实例的生命周期图,每个Vue实例在被创建的时候都需要经过一系列的初始化过程,例如需要设置数据监听,编译模板,将实例挂载到DOM并在数据变化时更新DOM等.同时在这个过程中也会运行 ...

  7. Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效

    当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素.然而在设计过程中可能会出现许多的问题,比如当前绘制了一个 ...

  8. 跨平台 GUI可视化 网络调试工具

    mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...

  9. office2007安装时显示安装程序找不到 office.zh-cn\officeLR.cab怎么办

    根本原因是和VS2008有关解决方法如下:1. 找到vs2008安装程序(光盘,镜像文件,解压文件都一样),找到WCU文件夹在他里面找到WebDesignerCore文件夹,然后打开它找到WebDes ...

  10. 1.JDBC基础

    JDBC全称Java Database Connectivity,即Java数据库连接.(以下以MySQL为例,使用MySQL语句) Sun公司提供了标准JDBC API接口,没有实现具体类.各个数据 ...