Unique Paths 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/unique-paths/description/


Description

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.

Solution

  1. class Solution {
  2. int combinatorial(int big, int small) {
  3. unsigned long long res = 1;
  4. unsigned long long i = static_cast<unsigned long long>(big - small + 1);
  5. unsigned long long j = 1;
  6. for (; i <= big, j <= small; i++, j++) {
  7. res *= i;
  8. res /= j;
  9. }
  10. return static_cast<int>(res);
  11. }
  12. public:
  13. int uniquePaths(int m, int n) {
  14. return combinatorial(m + n - 2, min(m, n) - 1);
  15. }
  16. };

解题描述

这道题目其实只需要使用简单的数学求解。机器人每次只能向右或者向下走一步,如果是在m * n的网格中,则向右走要m - 1步,向下走要n - 1步。所以求所有路径的数目其实就是求一个组合数。即在m + n - 2步中取m - 1步。然后主要的问题是计算上面的问题:第一次WA是因为大数计算溢出。于是换了个算法,改成每次乘上一个数之后要除去一个数,而不是连乘后连除;第二次WA是因为从大数方向往小数方向进行计算的时候会出现不能整除的情况,只能是换成从小数向大数进行累积计算。

[Leetcode Week12]Unique Paths的更多相关文章

  1. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  2. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

  3. [LeetCode] 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 ...

  4. LeetCode 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 ...

  5. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

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

  6. [LeetCode] 63. Unique Paths II 不同的路径之二

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

  7. [LeetCode] 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 ...

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

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

  9. 【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 ...

随机推荐

  1. MAC搭建 PHP 环境

    安装homebrew homebrew是mac下非常好用的包管理器,会自动安装相关的依赖包,将你从繁琐的软件依赖安装中解放出来. 安装homebrew也非常简单,只要在终端中输入11: 1 ruby ...

  2. JDK各个版本比较 JDK5~JDK9

    JDK5 自动装箱与拆箱: 枚举 静态导入,如:import staticjava.lang.System.out 可变参数(Varargs) 内省(Introspector),主要用于操作JavaB ...

  3. bzoj 1103: [POI2007]大都市meg (dfs序)

    dfs序,加个bit维护前缀和就行了 type arr=record toward,next:longint; end; const maxn=; var edge:..maxn]of arr; bi ...

  4. POJ2774:Long Long Message——题解

    http://poj.org/problem?id=2774 给定两个字符串 A 和 B,求最长公共子串. 论文题,把两个串合并起来,比较两个串各自的后缀的height值取最大即可. #include ...

  5. BZOJ4299 & CC FRBSUM:ForbiddenSum & BZOJ4408 & 洛谷4587 & LOJ2174:[FJOI2016]神秘数——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4299 https://www.lydsy.com/JudgeOnline/problem.php? ...

  6. GCJ2008 APAC local onsites C Millionaire

    自己Blog的第一篇文章,嗯... 接触这道题,是从<挑战程序设计竞赛>这本书看来的,其实头一遍读题解,并没有懂.当然现在已经理解了,想想当初可能是因为考虑两轮的那张概率图的问题.于是决定 ...

  7. 【初级算法】2.买卖股票的最佳时机 II

    题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必 ...

  8. 中国MOOC_面向对象程序设计——Java语言_第2周 对象交互_秒计时的数字时钟

    第2周编程题 查看帮助 返回   第2周编程题,在课程所给的时钟程序的基础上修改 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系 ...

  9. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  10. ZOJ 2532 Internship 求隔边

    Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB CIA headquarter collects data from acro ...