https://oj.leetcode.com/problems/unique-paths/

首先,转换成一个排列组合问题,计算组合数C(m+n-2) (m-1),请自动想象成上下标。

class Solution {
public:
int uniquePaths(int m, int n) {
if(m == && n == )
return ;
int sum1 = ;
int sum2 = ;
for(int i = ; i <= m-; i++)
sum1 *= i;
for(int j = m+n-; j >= n; j--)
sum2 = sum2*j;
return sum2/sum1;
}
};

runtime error,当测试数据是36,7的时候,也就是说,这个数太大了,已经算不了了。

于是参考了discuss

class Solution {
public:
int uniquePaths(int m, int n) {
if(m == && n == )
return ;
int sum1 = ;
int sum2 = ;
//exchange;
int temp;
if(m<n)
{
temp = n; n = m; m = temp;
}
int p,q;
int commonFactor;
for(int i = ; i<= n-; i++)
{
p = i;
q = i+m-;
commonFactor = gcd(p,q);
sum1 = sum1 * (p/commonFactor);
sum2 = sum2 * (q/commonFactor);
commonFactor = gcd(sum1,sum2);
sum1 = sum1/commonFactor;
sum2 = sum2/commonFactor;
} return sum2/sum1;
} int gcd(int a, int b)
{
while(b)
{
int c = a%b;
a = b;
b = c;
}
return a;
}
};

输入58,61的时候wa,因为结果得了个负数,明显又溢出了。

于是:

#include <iostream>
using namespace std; class Solution {
public:
int uniquePaths(int m, int n) {
if(m == && n == )
return ;
long long sum1 = ;
long long sum2 = ;
//exchange;
int temp;
if(m<n)
{
temp = n; n = m; m = temp;
}
int p,q;
int commonFactor;
for(int i = ; i<= n-; i++)
{
p = i;
q = i+m-;
commonFactor = gcd(p,q);
sum1 = sum1 * (p/commonFactor);
sum2 = sum2 * (q/commonFactor);
commonFactor = gcd(sum1,sum2);
sum1 = sum1/commonFactor;
sum2 = sum2/commonFactor;
} return sum2/sum1;
} int gcd(long long a, long long b)
{
while(b)
{
int c = a%b;
a = b;
b = c;
}
return a;
}
}; int main()
{
Solution myS;
cout<<myS.uniquePaths(,);
return ;
}

中间过程中使用了long long 类型。

记住求公约数的算法。

LeetCode OJ--Unique Paths *的更多相关文章

  1. 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). ...

  2. [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 ...

  3. 【题解】【排列组合】【素数】【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 ...

  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 Week12]Unique Paths II

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

  7. [Leetcode Week12]Unique Paths

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

  8. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. leetcode 【 Unique Paths 】python 实现

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

  10. [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 ...

随机推荐

  1. 【全面】Linux基础知识和基本操作语句大全(一)

    接触Linux已经有一段时间了,由于实际需要,三三两两地掌握了一些基本语法和实用语句,主要都是在日常开发中用得比较多的,条理不是特别清晰,请见谅!下面开始上硬货!! 基本操作: 关闭Linux系统的命 ...

  2. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  3. CentOS 7.X 中systemctl命令用法详解

    systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体.可以使用它永久性或只在当前会话中启用/禁用服务,下面来看CentOS 7.X 中 ...

  4. centos7 安全配置

    CentOS是最多人用来运行服务器的 Linux 版本,最新版本是 CentOS 7.当你兴趣勃勃地在一台主机或 VPS 上安装 CentOS 7 后,首要的工作肯定是加强它的安全性,以下列出的七件事 ...

  5. cf 1006E

    #include <iostream> #include <cstdio> #include <cstring> #include <string> # ...

  6. meta-data

    <meta-data android:name="string"   android:resource="resource specification"  ...

  7. JAVA 基础--开发环境Sublime Text 3 搭建

    方法一  打开Sublime Text 3,依次点击Preference, Browse Packages,在打开的窗口中双击User文件夹,新建文件JavaC.sublime-build,用记事本打 ...

  8. python列出指定目录下的所有目录和文件

    import os import docx def scanfile(rootdir): result = [] for f in os.walk(rootdir): for files in f[2 ...

  9. Tensorflow 笔记 -- tensorboard 的使用

    Tensorflow 笔记 -- tensorboard 的使用 TensorFlow提供非常方便的可视化命令Tensorboard,先上代码 import tensorflow as tf a = ...

  10. [笔记]Docker解决了什么问题?

    Docker的优势: 环境依赖问题 更轻量的虚拟化,节省了虚拟机的性能损耗 Docker应用场景: 程序分发,gitlab的安装很恶心吧,所以有人做了gitlab的image 部署发布,这点对运维的同 ...