LC 592. Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2
, you need to change it to the format of fraction that has denominator 1
. So in this case, 2
should be converted to 2/1
.
Example 1:
Input:"-1/2+1/2"
Output: "0/1"
Example 2:
Input:"-1/2+1/2+1/3"
Output: "1/3"
Example 3:
Input:"1/3-1/2"
Output: "-1/6"
Example 4:
Input:"5/3+1/3"
Output: "2/1"
Note:
- The input string only contains
'0'
to'9'
,'/'
,'+'
and'-'
. So does the output. - Each fraction (input and output) has format
±numerator/denominator
. If the first input fraction or the output is positive, then'+'
will be omitted. - The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
- The number of given fractions will be in the range [1,10].
- The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
class Solution {
public:
long long gcd(long long a, long long b) {
if(a < b) return gcd(b,a);
if(b == ) return a;
return gcd(b, a%b);
}
string fractionAddition(string expression) {
vector<int> numerator;
vector<int> denominator;
int j = ;
bool positive = false;
if(expression[] >= '' && expression[] <= '') positive = true;
else {
positive = false;
j = ;
}
string tmp;
for(size_t i=j+; i<expression.size(); i++) {
if(expression[i] == '+' || expression[i] == '-') {
tmp = expression.substr(j, i-j);
size_t k;
for(k = ; k < tmp.size(); k++) {
if(tmp[k] == '/') break;
}
numerator.push_back(stoi(tmp.substr(,k)));
if(!positive) numerator[numerator.size()-] *= -;
positive = expression[i] == '+' ? true : false;
denominator.push_back(stoi(tmp.substr(k+)));
j = i+;
}
}
tmp = expression.substr(j,expression.size()-j);
size_t k;
for(k=; k<tmp.size(); k++)
if(tmp[k] == '/') break;
numerator.push_back(stoi(tmp.substr(,k)));
if(!positive) numerator[numerator.size()-] *= -;
denominator.push_back(stoi(tmp.substr(k+)));
// test long long ret_numer = numerator[];
long long ret_denomi = denominator[];
long long r = ;
for(size_t i= ; i<denominator.size(); i++) {
long long tmp_deno = ret_denomi;
ret_denomi *= denominator[i];
ret_numer = ret_numer * denominator[i] + tmp_deno * numerator[i];
}
if(ret_numer > ) {
r = gcd(ret_numer, ret_denomi);
ret_numer /= r;
ret_denomi /= r;
} else if (ret_numer < ) {
r = gcd(-ret_numer, ret_denomi);
ret_numer /= r;
ret_denomi /= r;
} else if(ret_numer == ) {
ret_denomi = ;
}
string str_numer = ret_numer < ? ("-" + to_string(-ret_numer)) : to_string(ret_numer);
string str_deno = to_string(ret_denomi);
return str_numer + "/" + str_deno;
}
};
更好的一个思路
class Solution {
public:
string fractionAddition(string expression) {
istringstream iss(expression);
int num = , den = , NUM = , DEN = ;
char c;
while (iss >> num >> c >> den)
{
NUM = NUM * den + num * DEN;
DEN *= den;
int g = abs(gcd(NUM, DEN));
NUM /= g;
DEN /= g;
} return to_string(NUM) + "/" + to_string(DEN);
} int gcd(int x, int y)
{
return y == ? x : gcd(y, x % y);
}
};
LC 592. Fraction Addition and Subtraction的更多相关文章
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- 【leetcode】592. Fraction Addition and Subtraction
题目如下: 解题思路:本题考察的是分数的加减法.小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可.所以,本题只要按+,-两个符号分割输入 ...
- [LeetCode] Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [leetcode-592-Fraction Addition and Subtraction]
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 大数据加减(Big data addition and subtraction)
题目描述 Description 加减法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(不超过1000位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...
- Arc066_E Addition and Subtraction Hard
传送门 题目大意 给定一个加减法的表达式,让你任意的添加合法的括号对,使的表达式最大. 题解 考虑到任意左括号一定加在减号右边,那么对于第一个左括号,与该左括号相邻的只含有加号的子序列的贡献一定为负, ...
随机推荐
- MVC方式显示数据(数据库)
新建实体数据模型 选择ADO.NET实体数据模型,名称改为数据库名 因为使用现有数据库,所以选择来自数据库的EF设计器,只演示所以只选择一个表,空模型可后期增加表 选择从数据库更新模型 新建数据库连接 ...
- 【Zookeeper】分布式锁
一.概述 实现原理 实现代码 一.概述 分布式锁解决方案(目的:为了保证在分布式领域中共享数据安全问题) 数据库实现分布式锁(不推荐.效率特别低) 基于Redis实现分布式锁setNx (非常麻烦考虑 ...
- MySQL在command line Client下的一些命令
MySQL在command line Client下的一些命令 通过CMD进入到本地数据库: mysql -h localhost -u -root -p 参数说明: -h 要连接的服务器的主机名或I ...
- 用js刷剑指offer(树的子结构)
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 牛客网链接 js代码 /* function TreeNode(x) { this.val = x ...
- cmake升级3.6
https://blog.csdn.net/u013714645/article/details/77002555 ./boostrap gmake gmake install
- TCP中的长连接和短连接(转载)
原文地址:http://www.cnblogs.com/onlysun/p/4520553.html 次挥手,所以说每个连接的建立都是需要资源消耗和时间消耗的 示意图: ...
- WordCount--实现字符,单词,代码统计
Github: https://github.com/whoNamedCody/WordCount PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计 ...
- sqlserver2017安装Linux版教程
安装 SQL Server 下载 Microsoft SQL Server 2017 Red Hat 存储库配置文件: sudo curl -o /etc/yum.repos.d/mssql-serv ...
- C# 加载显示ftp上图片方法
ftp://用户名:密码@IP/路径 FTP的一种登陆方式 如ftp://sing:song@192.168.1.133/upload/pic/236.jpg
- 用Qt 画一个心形
MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTi ...