题目:

Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.

The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.

Example 1:

Input: expression = "-1/2+1/2"
Output: "0/1"

Example 2:

Input: expression = "-1/2+1/2+1/3"
Output: "1/3"

Example 3:

Input: expression = "1/3-1/2"
Output: "-1/6"

Constraints:

  • The input string only contains '0' to '9''/''+' and '-'. So does the output.
  • Each fraction (input and output) has the 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.

思路:

单纯模拟题,需要掌握两点,一个是最大公约数,用辗转相除法即可,但是要注意-1,比较的时候需要用abs;另一个是。首先需要四个数,up表示当前数分子,down表示当前数分母,当前数即up / down,初始化肯定是0,但是避免分母为0,我们把up初始化为0,down初始化为1,不影响结果;同理使用nextup和nextdown记录下一个数,用sign记录下一个数的正负,初始化为1即可。另外需要一个数为cur,记录当前数字,因为可能是111,则需要每次进位,即每次cur * 10 + 当前expression[i]的int即可。之后遍历expression,每次遇到加号或者减号就进行一轮计算,首先如果下个数字的分母是0,我们赋为1,防止分母为0。之后为nextup计算符号,套用公式获得新的up和down即可。这里记得重置cur和sign,因为sign是代表下一个数字的正负,因此当前expression[i]的加减号是当计算完新的up和down以后才赋值给sign。最后在遍历完成后加上最后一个数,再得出gcd,对up和down都除以最大公约数,再转成string就是答案了。

代码:

class Solution {
public:string fractionAddition(string expression) {int up = 0, down = 1;int i = 0, sign = 1;int nextup = 0, nextdown = 1;int cur = 0;while (i < expression.size()) {if (expression[i] == '-') {nextdown = cur == 0 ? 1 : cur;nextup *= sign;up = up * nextdown + down * nextup;down = down * nextdown;sign = -1;cur = 0;} else if (expression[i] == '+') {nextdown = cur == 0 ? 1 : cur;nextup *= sign;up = up * nextdown + down * nextup;down = down * nextdown;sign = 1;cur = 0;} else if (expression[i] == '/') {nextup = cur;cur = 0;} else {cur = cur * 10 + (expression[i] - '0');}i++;}nextdown = cur == 0 ? 1 : cur;nextup *= sign;up = up * nextdown + down * nextup;down = down * nextdown;sign = up >= 0 ? 1 : -1;int g = abs(gcd(up, down));up /= g;down /= g;string ans = to_string(up) + '/' + to_string(down);return ans;}
private:int gcd(int a, int b) {if (abs(a) < abs(b))return gcd(b, a);if (b == 0)return a;return gcd(a % b, b);}
};

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 All in One 题目讲解汇总(持续更新中...)

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

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

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

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

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

  5. Paper:GPT-3《 Language Models are Few-Shot Learners》的翻译与解读

    Paper:GPT-3< Language Models are Few-Shot Learners>的翻译与解读 目录 <GPT-3: Language Models are Fe ...

  6. css参考手册css3手册_CSS手册:面向开发人员CSS便捷指南

    css参考手册css3手册 I wrote this article to help you quickly learn CSS and get familiar with the advanced ...

  7. delphi 中的浮点数 (转载)

    原文地址 Floating point numbers - Sand or dirt Floating point numbers are like piles of sand; every time ...

  8. ICLR2020国际会议焦点论文(Spotlight Paper)列表(内含论文源码)

    来源:AINLPer微信公众号(点击了解一下吧) 编辑: ShuYini 校稿: ShuYini 时间: 2020-02-21     2020年的ICLR会议将于今年的4月26日-4月30日在Mil ...

  9. Commons 组件学习笔记

    目录 目录 Commons 组件在apache官网有详细的资料深入学习的可以在官网查询API及相关资料 Commons 介绍 Commons Lang 组件简介 数组元素的增加 AarryUtils类 ...

  10. Paper:GPT-3之《 Language Models are Few-Shot Learners》的翻译与解读

    Paper:GPT-3之< Language Models are Few-Shot Learners>的翻译与解读 目录 <GPT-3: Language Models are F ...

最新文章

  1. [maven] 使用问题及思考汇总
  2. Android -- 消息处理机制源码分析(Looper,Handler,Message)
  3. jQuery中终止Ajax请求
  4. 前线观察 | AWS re:Invent 2018见闻实录
  5. python网页优化公司_使用python优化scipy.optimize.minimize公司
  6. grep与sed批量处理多个文件中的字符串的方法
  7. 使用Java 8流进行快速失败的验证
  8. php置顶文章,zblogphp调用置顶文章的教程
  9. iPhone 6S三大性能实测
  10. centos杀死进程命令
  11. Android数据库一些源码分析
  12. 袁承兴:【译】Async/Await(一)——多任务
  13. MediaWiki自动登陆和更新页面
  14. vue如何加载html字符串_VUE渲染后端返回含有script标签的html字符串示例
  15. 矩阵的逆及求逆矩阵的方法,可逆矩阵定理与判定方法,(非)奇异矩阵,方程Ax=b解法,Hilbert矩阵及其逆的求法,条件数(Condition Number)及其计算方法
  16. 5G移动通信网的定位技术发展趋势
  17. 组员组长mysql_GitHub - gzh51906/ManKeZhan: 组长:黄林芳 组员:陈炜,王佳伟
  18. Python使用RMF聚类分析客户价值
  19. 如何查看自己电脑的并口端口号?
  20. 千年疑惑:为什么我越累越失眠,越睡越疲惫?

热门文章

  1. c语言且不用 怎么表示,“并且”用C语言怎么表示
  2. [ Linux驱动炼成记 ] 12 -音频驱动TAS5754添加EQ参数
  3. 最新NVIDIA英伟达GPU显卡算力表
  4. 程序员是做什么的?未来计算机变得智能,就不需要程序员了吗?
  5. 发送、抄送、密送、分别发送、回复、回复全部、转发的区别(一篇文章研究透彻)
  6. java scene_JavaFX中场景(Scene)的意义是什么?
  7. 2020年证券从业《证券市场基本法律法规》真题汇编卷一
  8. 换机潮爆发,5G手机+5G超级SIM卡成趋势
  9. 现在能否办理5G卡?联通:尚未对公众客户开放办理
  10. puml绘制思维导图_盘点那些美美哒的在线思维导图制作神器