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 ...
随机推荐
- redis主从+哨兵模式
主从模式配置分为手动和配置文件两种方式进行配置,我现在有192.168.238.128(CentOS1).192.168.238.131(CentOS3).192.168.238.132(CentOS ...
- SQL Server ALwayson 正在解析
原因:把主库切换到辅助副本以后,集群全部出现正在解析的情况,数据库显示“恢复挂起” 过程:把服务器重启,原以为正在解析会恢复正常.结果失败. 解决方法:出现“正在解析”的情况跟故障转移群集有关,进故障 ...
- Django与多个数据库交互
定义数据库 在Django中使用多个数据库的第一步是告诉Django您将要使用的数据库服务器. 数据库可以有您选择的任何别名.但是,别名 default 有着特殊的意义.Django使用别名为 def ...
- How to setup multimedia on CentOS 7
You will need to also install the EPEL repository as nux-dextop depends on this for some of its pack ...
- 1036: [ZJOI2008]树的统计Count(树链剖分)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 19830 Solved: 8067[Submit ...
- 和为n连续正数序列 【微软面试100题 第五十一题】
题目要求: 输入一个正数n,输出所有和为n连续正数序列(至少两个). 例如输入15,由于1+2+3+4+5 = 4+5+6 = 7+8 = 15.所以输出3个连续序列1~5,4~6,7~8. 参考资料 ...
- Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)
题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果: 解题思路: 解法还是很简单的,但是需要注意以下几点: 1. 如果两个链表都空,则返回null; 2. 如果链表1空,则返回链表2的 ...
- python学习-- {% csrf_token %}
1.不推荐禁用掉django中的CSRF. 2.我们可以再html页面的form表单中添加csrf_token,带着表单的请求一起发送到服务器去验证. <form enctype=" ...
- dubbo控制台在tomcat上的部署
1.下载dubbo-admin的war包,比如dubbo-admin-2.5.4.war 2.因为在tomcat上部署,所以务必确认安装了JDK和tomcat,以及配置好了环境变量. 3.将dubbo ...
- C语言总结(3)
1.字符输入函数getchar 输入一个字符 char ch; ch=getchai(); 字符输出函数putchar 输出一个字符 putchar(输出参数): 2.调用scanf和printf输入 ...