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. UVA 10790 How Many Points of Intersection? 组合数学

    We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segment ...

  2. 【转载】什么是NVMe?

    什么是NVMe? [转载]什么是NVMe:http://storage.it168.com/a2018/0921/5045/000005045252.shtml NVMe是Non-Volatile M ...

  3. EF CodeFirst 使用T4模板

    实用等级:★★★★★ 首先,定义一个接口,代表一个领域实体.在定义一个实体集成这个接口,面向接口编程的各种好处就不提了. /// <summary> /// 代表一个领域实体 /// &l ...

  4. WebService 创建、发布、调用

    环境Win7+VS2017 启用IIS 查看iis是否启用 新建 ASP.NET Web 应用程序 项目,项目中添加Web 服务 在 asmx  文件中添加需要的方法 运行结果 发布 创建新的文件夹, ...

  5. 转 Fortofy扫描漏洞解决方案

    Log Forging漏洞: 数据从一个不可信赖的数据源进入应用程序. 在这种情况下,数据经由CreditCompanyController.java 的第 53行进入 getParameter(). ...

  6. 实用的生成二维码API

    推荐一个生成二维码的API网站: 中文 http://www.topscan.com 英文 https://api.qrserver.com/v1/create-qr-code/?size=150x1 ...

  7. 如何使用 Set 来提高JS代码的性能

    摘要: 高效使用Set! 作者:前端小智 原文:如何使用 Set 来提高代码的性能 Fundebug经授权转载,版权归原作者所有. 为了保证的可读性,本文采用意译而非直译. 我确信有很多开发人员坚持使 ...

  8. Django 练习班级管理系统七 -- 编辑老师列表(二)

    修改 views.py @auth def edit_teacher(request, nid): if request.method == "GET": obj = models ...

  9. Linux 读写锁

    线程的读写锁函数: 1,读写锁的初始化与销毁,静态初始化的话,可以直接使用PTHREAD_RWLOCK_INITIALIZER. #include <pthread.h> int pthr ...

  10. Linux编译安装相关命令

    1.  configuer configure  是一个shell脚本,用来检测安装平台是否符合要求,并通过配置参数生成makefile文件 2. make  (make all 的简写) 编译命令, ...