[LeetCode] 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.
这道题让我们做分数的加减法,给了我们一个分数加减法式子的字符串,然我们算出结果,结果当然还是用分数表示了。那么其实这道题主要就是字符串的拆分处理,再加上一点中学的数学运算的知识就可以了。这里我们使用字符流处理类来做,每次按顺序读入一个数字,一个字符,和另一个数字。分别代表了分子,除号,分母。我们初始化分子为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
LeetCode All in One 题目讲解汇总(持续更新中...)
[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 ...
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- LC 592. 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
题目如下: 解题思路:本题考察的是分数的加减法.小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可.所以,本题只要按+,-两个符号分割输入 ...
- [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- [leetcode-592-Fraction Addition and Subtraction]
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
随机推荐
- pytest 常见用法
前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...
- Vue router-link路由不同的写法,不一样的效果
我想要的路径:http://localhost:8080/#/main/hato/realtime/eventDetail/238 情况一:未进行路由配置: { path: 'eventD ...
- axios 源码分析(上) 使用方法
axios是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它可以在浏览器和node环境下运行,在github上已经有六七万个星了,axios使用很方便,很多人在使用他,vu ...
- mysql启动报错:Failed to start LSB: start and stop MySQL
报错信息: [root@youxx- bin]# service mysql status Redirecting to /bin/systemctl status mysql.service ¡ñ ...
- How-important-is-deep-learning-in-autonomous-driving
Deep learning (DL) is a very interesting technology indeed and yes it does solve perception really w ...
- POC挖矿没有前途
最好的工作量证明是能力证明,能力不适合存储.望文生义,能力的本质意义是“能”和“力”(所有的词汇都按照望文生义理解,因为所有不能望文生义的词汇都是不良的已经被前人修正或将来被后人修正)能力是流动的,迁 ...
- nodejs-翻转算法
nodejs-翻转算法 /** * Created by moon on 2019/12/14. */ //程序运行完成时一定要有输出语句,本工具才能正确展示运行结果. function abc() ...
- tf.reduce_sum() and tf.where()的用法
import tensorflow as tfimport numpy as npsess=tf.Session()a=np.ones((5,6))c=tf.cast(tf.reduce_sum(a, ...
- Vue新手入门教程
谈谈我对Vue的理解 vue就是前端上的Java,前端上的C#.有个前端的虚拟DOM引擎,设计理念和Java,C#类似.我们只需要告诉DOM应该显示什么,而不用去操作DOM元素. 如何引用? 下面是一 ...
- DOIS2019大会,腾讯 DevOps 测试中台探秘
WeTest 导读 腾讯WeTest受邀参加2019年DevOps 国际峰会,由WeTest产品负责人-殷柱伟老师分享了腾讯DevOps测试中台的实践经验与心得, 议题受到了现场听众及行业媒体的关注与 ...