LeetCode OJ 62. Unique Paths
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的更多相关文章
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【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 ...
- LeetCode OJ:Unique Paths II(唯一路径II)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- [leetcode DP]62.Unique Paths
判断一个物体从左上角到右下角有多少种走法 class Solution(object): def uniquePaths(self, m, n): flag = [[1 for j in range( ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
随机推荐
- 各类数据库url
msql: jdbc:mysql://127.0.0.1:3306/databaseName ms-sql jdbc:microsoft:sqlserver://127.0.0.1:1433;Data ...
- Vim编辑器与Shell命令脚本
章节简述: 本章节将教给您如何使用Vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库 ,熟练使用各个模式和命令快捷键. 我们可以通过Vim编辑器将Linux命令放入合适的逻辑测试语句(if. ...
- idea不能加载xml
- myBatis系列之七:事务管理
myBatis系列之七:事务管理 如果在操作时,如果运行时错误自动进行回滚,需要以下两个配置 @Transactional()public void save(User user) { userDao ...
- 解决 spring mvc 3.+ 结合 hibernate3.+ 使用<tx:annotation-driven>声明式事务无法提交的问题
spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 <tx:annotation-driven transaction- ...
- CodeForces 706A Beru-taxi
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- 工厂模式Assembly.Load(path).CreateInstance 反射出错解决办法
项目结构: DALFactory 反射代码反射 //使用缓存 private static object CreateObject(string AssemblyPath,string classNa ...
- Java 并发 线程的优先级
Java 并发 线程的优先级 @author ixenos 低优先级线程的执行时刻 1.在任意时刻,当有多个线程处于可运行状态时,运行系统总是挑选一个优先级最高的线程执行,只有当线程停止.退出或者由于 ...
- ElasticSearch(7)-排序
引用自ElaticSearch权威指南 一.排序 相关性排序 默认情况下,结果集会按照相关性进行排序 -- 相关性越高,排名越靠前. 这一章我们会讲述相关性是什么以及它是如何计算的. 在此之前,我们先 ...
- mac上搭建python+selenium2的环境
1.mac默认已安装有python和easy_install 2.进入终端,使用root的权限,然后输入,回车后需要root的密码,即可安装成功,成功结果如下所示 sudo easy_install ...