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:

  1. The input string only contains '0' to '9''/''+' and '-'. So does the output.
  2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
  3. 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.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

这道题让我们做分数的加减法,给了我们一个分数加减法式子的字符串,然我们算出结果,结果当然还是用分数表示了。那么其实这道题主要就是字符串的拆分处理,再加上一点中学的数学运算的知识就可以了。这里我们使用字符流处理类来做,每次按顺序读入一个数字,一个字符,和另一个数字。分别代表了分子,除号,分母。我们初始化分子为0,分母为1,这样就可以进行任何加减法了。中学数学告诉我们必须将分母变为同一个数,分子才能相加,为了简便,我们不求最小公倍数,而是直接乘上另一个数的分母,然后相加。不过得到的结果需要化简一下,我们求出分子分母的最大公约数,记得要取绝对值,然后分子分母分别除以这个最大公约数就是最后的结果了,参见代码如下:

class Solution {
public:
string fractionAddition(string expression) {
istringstream is(expression);
int num = , dem = , A = , B = ;
char c;
while (is >> num >> c >> dem) {
A = A * dem + num * B;
B *= dem;
int g = abs(gcd(A, B));
A /= g;
B /= g;
}
return to_string(A) + "/" + to_string(B);
}
int gcd(int a, int b) {
return (b == ) ? a : gcd(b, a % b);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/592

参考资料:

https://leetcode.com/problems/fraction-addition-and-subtraction/

https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/103388/Concise-Java-Solution

https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/103384/Small-simple-C%2B%2BJavaPython

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 592. Fraction Addition and Subtraction 分数加减法的更多相关文章

  1. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  2. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

  3. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  4. LC 592. Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  5. 【leetcode】592. Fraction Addition and Subtraction

    题目如下: 解题思路:本题考察的是分数的加减法.小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可.所以,本题只要按+,-两个符号分割输入 ...

  6. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  7. [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  8. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  9. [leetcode-592-Fraction Addition and Subtraction]

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

随机推荐

  1. javascript json写法

    javascript json写法 var shuxing = {name:"super",sex:"19",work:"IT"}; 这个k ...

  2. 怎么做web接口测试

        这就需要开发提供的接口文档了,接口文档和功能测试的需求说明书的功能是一样的.包括:接口说明.调用的url,请求方式(get or post),请求参数.参数类型.请求参数说明,返回结果说明.有 ...

  3. 下载安装office2019

    Hello,大家好,我是小喵. 支付宝搜索“321994”,领红包喽! 前几天答应给大家写一篇关于安装激活Office2019的文章.一直在准备,准备制作GIF动图,制作图片等,把我电脑上的Offic ...

  4. XtraReport报表入库单数字转中文大写数字

    先看看打印入库单的效果图,看如下: 客户要求合计一行,要求大写中文数字.XtraReport报表是如何做出以上图的效果呢?因为是要把数字转成大写中文数字,得先准备数字转大写中文数字的函数.因网上有很多 ...

  5. img error 图片加载失败的最佳方案

    有时候, 当img的src加载失败, 会显示缺省碎片图片,  影响用户体验.  有一个js事件onerror就派上了用场. 它可以在加载失败时, 显示缺省的图片. 它有两种使用方式. 第一种: 使用纯 ...

  6. wordpress 数据查询-全局注入-模板数据消费输出简图

    我一直比较好奇,类似于wordpress这样的CMS,它可以做的很灵活,同样的软件,为什么就能做出几乎完全不具有相似性的不同站点来呢?除了功能可以有大不同以外,即便是相同的简单blog站他们的外观也可 ...

  7. pip下载速度慢解决方法

    添加镜像链接 解决方式: 更改pip的数据源.目前国内比较知名的有豆瓣的,清华的.都是pipy官网的镜像. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里 ...

  8. 关于BFS+异或(C++)

    今天早上,我们做了场比赛,里面有一个题目是这样的.. 题目大意:        随着马场的繁荣,出现了越来越多的新马种.种族之间的沟通不畅严重影响了马场的和谐.这时,科学家发明了马语翻译机器人,正好可 ...

  9. vs2015 创建MVC项目

    直接上图吧! 第一步:新建项目 第二步:选择模板 第三步:系统自动生成项目文件 第四步:创建控制器(C):找到Controllers文件夹->右键->添加->控制器 第五步:添加控制 ...

  10. logstash 入门篇

    场景介绍 基于分布式集群海量日志数据,且分布在不同的服务器上,日志的采集以及可视化是需要我们解决的问题.ELK就是这么一个方案,当然我们这里主要讲解logstash安装配置和基础语法. ELK帮我们解 ...