题目链接:https://leetcode.com/problems/fraction-addition-and-subtraction/#/description

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.

思路:Keep the overall result in A / B, read the next fraction into a / b. Their sum is (Ab + aB) / Bb (but cancel their greatest common divisor).

方法一:

class Solution {
public:string fractionAddition(string expression) {istringstream ss(expression);int A = 0, B = 1, a, b;char _;while (ss >> a >> _ >> b) {A = A * b + a * B;B *= b;int g = abs(GCD(A, B));A /= g;B /= g;}return to_string(A) + '/' + to_string(B);}
private:int GCD(int a, int b ){return (b == 0) ? a : GCD(b, a % b);}};

方法二:

class Solution {
public:string fractionAddition(string expression){int n = 0, d = 1, p = 0, p1 = 0, p2 = 0; // n为上一个分子,d为上一个分母if (expression[0] != '-') expression = "+" + expression;while (p < expression.size()){for (p1 = p + 1; expression[p1] != '/'; ++p1);for (p2 = p1 + 1; p2 < expression.size() && expression[p2] != '+' && expression[p2] != '-'; ++p2);auto nn = stoi(expression.substr(p + 1, p1 - p - 1)), dd = stoi(expression.substr(p1 + 1, p2 - p1 - 1));// nn为当前分子,dd为当前分母cout<<nn<<"  "<<dd<<endl;n=n*dd+(expression[p] == '-' ? -1 : 1)*nn*d;d*=dd;int g=abs(GCD(n,d));n/=g;d/=g;p=p2;}return to_string(n) + "/" + to_string(d);}
private:int GCD(int a, int b ){return (b == 0) ? a : GCD(b, a % b);}};

[leetcode]592. Fraction Addition and Subtraction相关推荐

  1. leetcode 592. Fraction Addition and Subtraction | 592. 分数加减运算(最大公因数gcd,最小公倍数lcm)

    题目 https://leetcode.com/problems/fraction-addition-and-subtraction/ 题解 这题既简单又麻烦,一道 hard 的 easy 题,被划分 ...

  2. Leetcode 592. 分数加减运算 C++

    Leetcode 592. 分数加减运算 题目 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 ...

  3. LeetCode 592. 分数加减运算(字符串+最大公约数)

    1. 题目 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分母 ...

  4. 图解LeetCode——592. 分数加减运算(难度:中等)

    一.题目 给定一个表示分数加减运算的字符串 expression,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即:最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成 ...

  5. Leetcode 166. Fraction to Recurring Decimal

    这个是我比较原始的解法.在这里有个trick就是把int扩展为long.要考虑负数情况,以及负数最小值的情况. public class Solution {public String fractio ...

  6. leetcode 598. Range Addition II | 598. 范围求和 II

    题目 https://leetcode-cn.com/problems/range-addition-ii/ 题解 经过观察发现,最大元素会是两个操作对应矩阵的交集区域. class Solution ...

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

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  8. oracle时间怎么相加减,Oracle 如何对时间进行简单加减运算

    在我们用dbms_job包进行定时Job的时候,需要设置时间间隔,所以需要知道时间的基本加减方法. SQL> alter session set nls_date_format='yyyy-mm ...

  9. c语言中减号算一个字符吗,C语言中指针的加减运算

    char arr[3]; printf("arr:\n%d\n%d\n%d\n", arr, arr + 1, arr + 2); char *parr[3]; printf(&q ...

  10. 寒假LeetCode打卡

    文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...

最新文章

  1. GoAccess安装及分析nginx实时日志
  2. 中盐总公司:盐业公司24小时配送保供应
  3. 34.2. terminal
  4. 数据结构 --静态队列 讲解
  5. URL重写后,在有页面回发时的处理
  6. 使用vSAN RVC进一步了解vSAN环境
  7. SAP License:物料账差异
  8. mysql密码正确却提示错误,不使用密码反而能登录
  9. 低微漏洞处理办法记录
  10. 力扣每日一刷-144,二叉树前序遍历-递归解法
  11. verilog学习 (二)
  12. GitHub使用教程详解(上)——官网操作指南[翻译]
  13. QT QListView
  14. 【python中级】linux系统获得计算机网卡流量
  15. 如何用阿里云服务器建立个人网站
  16. good website
  17. Unity 关于Activator.CreateInstance使用
  18. 拳王公社:虚拟资源项目赚钱方法?前2种最常见,第3种鲜为人知
  19. pygame游戏素材预处理
  20. linux删除文件的前n行

热门文章

  1. 用html制作四行四列的表格,HTML表格
  2. 小程序审核规则大致内容
  3. oracle恢复删除的表
  4. 腾讯云服务器安全吗?来说说
  5. 电容有哪些用途?常见的九大作用
  6. 演练 畅销书排行榜 1002 html
  7. python制作脑图_使用Python将xmind脑图转成excel用例(一)
  8. no version information available问题解决
  9. c语言指数公式_c语言指数函数
  10. 【程序员(媛)国人之光】知(美)识(色)贩卖贴】非标题党】