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.


【题目分析】

给定一个m*n的grid,机器人从左上角出发到达右下角,每次只能向右或者向下移动一步,则到达右下角的不同的路线有多少条?


【思路】

1. 组合数学的思想

在组合数学中关于这个问题有一个专门的讨论,作为组合数入门的一个引入题目。原题目是从坐标点(0, 0)出发,到达另外一个点(m, n)的不同路径有多少条,其中m >= 0, n>=0。

组合数学的思想是这样的,从(0, 0)到达(m, n),无论无论怎么走,总的步数是确定的:m + n,而且肯定是向右移动了m步,向上移动了n步。那么我们先选定向右移动的m步,则剩下的n步就是确定的。所以这是一个组合数C(m+n, m) 或者 C(m+n, n)。

2. 动态规划的思想

动态规划的思想就是在移动过程中计算出到达的每一个小格子的方法数。

(1)到达最上边和最右边方格的方法数是1;

(2)以后到达每一个方格的方法数由它左边和上边的方法数确定,因为我们只能向右和向下移动;结果如下:


【java代码】组合法——由于计算过程中可能会遇到很大的数,要注意溢出的问题。

 public class Solution {
public int uniquePaths(int m, int n) {
m--; n--;
int mn = m + n;
int num = Math.min(m ,n);
double ans = 1;
for(int i=0;i<num;i++)
ans = ans * ((double)(mn - i) / (num-i));
return (int)Math.round(ans);
}
}

【java代码】动态规划

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

LeetCode OJ 62. Unique Paths的更多相关文章

  1. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. LeetCode OJ 63. Unique Paths II

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

  4. 【LeetCode】62. Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  5. LeetCode OJ:Unique Paths II(唯一路径II)

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

  6. LeetCode OJ:Unique Paths(唯一路径)

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

  7. [leetcode DP]62.Unique Paths

    判断一个物体从左上角到右下角有多少种走法 class Solution(object): def uniquePaths(self, m, n): flag = [[1 for j in range( ...

  8. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

随机推荐

  1. float 属性详解

    当前元素分类(float:left) 下一个紧邻元素分类(不含float) 结论 块级元素(a) 块级元素(b) b会填充a遗留下来的空间,a会和b发生重叠,a的图层在上面. 内联元素(b) b会紧跟 ...

  2. 诡异的数学,数字问题 - leetcode

    134. Gas Station 那么这题包含两个问题: 1. 能否在环上绕一圈? 2. 如果能,这个起点在哪里? 第一个问题,很简单,我对diff数组做个加和就好了,leftGas = ∑diff[ ...

  3. C++四种cast操作符

    C 风格(C-style)强制转型如下: (T) expression  或 T(expression) //函数风格(Function-style) 两种形式之间没有本质上的不同. 对于具有转换的简 ...

  4. jQuery操作radio

    JQuery获取选中的radio $radio = $('input:radio[name="sex"][class="xxxx"]:checked') 获取n ...

  5. 修改searchBar的返回按钮的显示文字

    #pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦) - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { ...

  6. Scala 控制结构

    Scala内建的控制结构屈指可数,仅有if, while, for, match, try和函数调用而已. 几乎所有的Scala控制结构都会产生某个值(while和 do while虽然不能产生有意义 ...

  7. 如何在MyEclipse中配置jre的编译运行环境

    由于在MyEclipse中已经自带了jre编译环境,但由于版本太低,所以有时候需要将编译环境配置为系统的jre版本.在MyEclipse中配置jre的编译运行环境很简单,只需要全局配置一次,则所有项目 ...

  8. oracle中关于Oracle Database 11g Express Edition 打不开的问题

    报的错误是http://127.0.0.1:...什么的找不到该文件 如果是127.0.0.1没问题,而且oracle中5个服务没问题,而且oracle可以启动.. 最后的问题是8080端口冲突,如果 ...

  9. 运动框架实现思路(js)

    思路:速度.(改变left,right,width,height,opacity) 2.缓冲动画. 3.多物体运动. 4.任意值变化. 5.链式运动. 6.同时运动.

  10. Redis Cluster 实践

    一:关于redis cluster 1:redis cluster的现状 reids-cluster计划在redis3.0中推出,可以看作者antirez的声明:http://antirez.com/ ...