LeetCode OJ--Unique Paths *
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 *的更多相关文章
- 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). ...
- [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 ...
- 【题解】【排列组合】【素数】【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 ...
- 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 ...
- [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 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- [Leetcode Week12]Unique Paths
Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 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). ...
- [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 ...
随机推荐
- PAT 乙级 1088
题目 题目链接:PAT 乙级 1088 题解 比较简单的一道题,下面来简单说说思路: 因为甲确定是一个两位数,因此通过简单的暴力循环求解甲的值,又根据题设条件“把甲的能力值的 2 个数字调换位置就是乙 ...
- 【linux】文件默认权限:umask
在默认权限的属性上,目录与文件是不一样的.从第六章我们知道 x 权限对於目录是非常重要的! 但是一般文件的创建则不应该有运行的权限,因为一般文件通常是用在於数据的记录嘛!当然不需要运行的权限了. 因此 ...
- 【Hadoop/Hive/mapreduce】系列之使用union all 命令之后如何对hive表格使用python进行去重
业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的: uid cate1 cate2 在hive QL中,我们知道union有着自动去重的功能,但是那是 ...
- python寻找模块的路径顺序
>>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...
- org.apache.catalina.webresources.Cache.backgroundProcess The background cache eviction process was unable to free [10] percent of the cache for Context [/filestore] - consider increasing the maximum s
需要耐心啊,太急于求成,希望直接就得到解决方法了...以至于正确方法都已经出现了,我却没有耐心看下去,所以反而又耽误了不少时间.... 项目加载100+张图片,还有一个小的MP4,所以console警 ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- Linux学习-进程管理
为什么进程管理这么重要呢? 这是因为: 首先,我们在操作系统时的各项工作其实都是经过某个 PID 来达成的 (包括你的 bash 环境), 因此,能不能进行某项工作,就与该进程的权限有关了. 再来,如 ...
- flask-博客文章
提交和显示博客文章 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key= ...
- BZOJ 4368: [IOI2015]boxes纪念品盒
三种路径,左边出去左边回来,右边出去右边回来,绕一圈 绕一圈的路径最多出现一次 那么绕一圈的路径覆盖的点一定是左边半圈的右边和右边半圈的左边 枚举绕一圈的路径的起始点(一定要枚举,这一步不能贪心),更 ...
- MySQL主从复制(Master-Slave)
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展.多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能. 下图就描述了一个多个数 ...