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. 【二分 最小割】cf808F. Card Game

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  2. java解析多层嵌套json字符串

    java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...

  3. salt 模板

    http://www.mamicode.com/info-detail-2297406.html

  4. 《linux设备驱动开发详解》笔记——7并发控制

    linux中并发无处不在,底层驱动需要考虑. 7.1 并发与竞争 7.1.1 概念 并发:Concurrency,多个执行单元同时.并行执行 竞争:Race Condistions,并发的执行单元对共 ...

  5. 实验二 JSP基本动态元素的使用

    实验二  JSP基本动态元素的使用 实验性质:验证性          实验学时:  2学时      实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...

  6. jmeter接口测试 ——学习笔记

    JMETER常用操作 1.jmeter做http脚本 Http请求页面内容介绍 添加cookie 线程组-添加-配置元件--HTTP Cookie管理器 添加权限验证 不能使用普通用户修改学生金币,接 ...

  7. Linux学习-备份策略

    每部主机的任务都不相同,重要的数据也不相同,重要性也不一样,因此,每个人的备份思考角度都不一样! 备份分为两大部分,一个是每日备份经常性变动的重要数据, 一个则是每周备份就不常变动的信息.这个时候我就 ...

  8. BZOJ 4057: [Cerc2012]Kingdoms

    状压DP #include<cstdio> #include<cstring> using namespace std; int F[1200005],A[25][25],st ...

  9. 修改const变量

    看下面的一段代码 ; int * j=(int*)(&i); // 运行正确,j确为i的地址,但 int *j=&i; 编译错误 *j=; //确实改变了i的值 printf(&quo ...

  10. No identifier specified for entity: XXXX 错误

    在运行项目的时候报了下面的错误: by: org.hibernate.AnnotationException: No identifier specified for entity: com.exam ...