[LeetCode] 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] Fraction Addition and Subtraction 分数加减法的更多相关文章
- [LeetCode] 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 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- [Swift]LeetCode592. 分数加减运算 | 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 ...
- 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
题目如下: 解题思路:本题考察的是分数的加减法.小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可.所以,本题只要按+,-两个符号分割输入 ...
- [LeetCode] Fraction to Recurring Decimal 分数转循环小数
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 ...
- LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
随机推荐
- 58 同城 iOS 客户端 iOS11 及 iPhone X 适配实践
一.前言 前段时间 WWDC 大会上苹果推出了 iOS11 系统 和 iPhone X 新机型,相信各个 iOS 团队的开发者都已经在计划新系统和新机型的适配工作了.不得不说,新系统和新机型的发布确实 ...
- hibernate框架学习笔记4:主键生成策略、对象状态
创建一个实体类: package domain; public class Customer { private Long cust_id; private String cust_name; pri ...
- 通过运行一个tomcat容器来记录下初学docker常用的几个命令---容器篇
1.查看容器列表 显示正在运行的容器: [root@localhost HMK]# docker ps 显示所有容器,包括未运行的: [root@localhost HMK]# docker ps - ...
- 第2次作业:软件分析之Steam
1. 作业内容 1.1 介绍产品相关信息 你选择的产品是? 我选择的分析的软件为STEAM 为什么选择该产品作为分析? 在上述列表中的产品,除了王者荣耀,其他几项都是平时我使用较为频繁的软件,所以最初 ...
- C语言博客作业--一二维数组。
一.PTA实验作业 题目1:7-1 将数组中的数逆序存放 1. 本题PTA提交列表 2. 设计思路 定义三个整型变量n用来存放整数个数i,j是循环数 scanf("%d",& ...
- 201621123060《JAVA程序设计》第二周学习总结
1.本周学习总结 本周学习了JAVA中的引用类.包装类(学习了一种语法:自动装箱)和数组(遍历数组的新方法foreach循环). 2. 书面作业 1.String-使用Eclipse关联jdk源代码 ...
- SOAP不同版本引起的问题
曾经遇到这样一个问题,在组织soap字符串时报这个错误: 2013-5-29 17:25:56 org.apache.cxf.phase.PhaseInterceptorChain doDefaul ...
- NOIP2016 天天爱跑步 80分暴力
https://www.luogu.org/problem/show?pid=1600 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养 ...
- python虚拟环境搭建大全(转)
Pipenv & 虚拟环境 本教程将引导您完成安装和使用 Python 包. 它将向您展示如何安装和使用必要的工具,并就最佳做法做出强烈推荐.请记住, Python 用于许多不同的目的.准确地 ...
- H5 音频标签自定义样式修改以及添加播放控制事件
说明: 需求要求这个音频标签首先要是可适配移动端浏览器的,音频样式就是参考微信做的. 最终效果如下: 具体实现 思路: H5 的 <audio> 标签是由浏览器负责实现默认样式的.所以不同 ...