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.

思路:

如下是参考一 大神给出的代码,先贴这儿,慢慢学习:

string fractionAddition(string s)
{
long p = , q = , p1, q1, t;
for (size_t i = , j; i < s.size(); i = j) {
j = s.find_first_of("+-", i+);
if (j == string::npos) j = s.size();
auto k = s.find('/', i);
long x = stol(s.substr(i, k-i)), y = stol(s.substr(k+, j));
p1 = p*y+q*x;
q1 = q*y;
t = __gcd(p1, q1);
p = p1/t;
q = q1/t;
if (q < ) p *= -, q *= -;
}
return to_string(p)+"/"+to_string(q);
}

如下是leetcode上的solution。

The initial fraction is 0/1 (n/d). We just need to read next fraction (nn/dd), normalize denominators between n/d and nn/dd (using GCD), and add/subtract the numerator (n +/- nn). In the end, we also need to use GCD to make the resulting fraction irreducible.

int GCD(int a, int b ){ return (b == ) ? a : GCD(b, a % b); }
string fractionAddition(string s) {
int n = , d = , p = , p1 = , p2 = ;
if (s[] != '-') s = "+" + s;
while (p < s.size()) {
for (p1 = p + ; s[p1] != '/'; ++p1);
for (p2 = p1 + ; p2 < s.size() && s[p2] != '+' && s[p2] != '-'; ++p2);
auto nn = stoi(s.substr(p + , p1 - p - )), dd = stoi(s.substr(p1 + , p2 - p1 - ));
auto gcd = GCD(d, dd);
n = n * dd / gcd + (s[p] == '-' ? - : ) * nn * d / gcd;
d *= dd / gcd;
p = p2;
}
auto gcd = GCD(abs(n), d);
return to_string(n / gcd) + "/" + to_string(d / gcd);
}

参考:

https://leetcode.com/maskray/

https://discuss.leetcode.com/topic/90024/c-12-lines-gcd

[leetcode-592-Fraction Addition and Subtraction]的更多相关文章

  1. [LeetCode] 592. 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. [LeetCode] Fraction Addition and Subtraction 分数加减法

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  7. [Swift]LeetCode592. 分数加减运算 | Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return t ...

  8. [LeetCode] 598. Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  9. [LeetCode] 370. Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

随机推荐

  1. list和map集合

    List特点:元素有放入顺序,元素可重复Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的)Map特点: ...

  2. 基于express+mongodb+pug的博客系统——pug篇

    很久之前就想自己搭一个博客了,最开始用hexo+github,但是换电脑后总是有些麻烦.后来使用WordPress,但是用WordPress总觉得没什么技术含量,前后端都是人家写好的,而且买的垃圾虚拟 ...

  3. jmeter IP欺骗功能实现

    使用过loadrunner的同学,应该都了解有个IP欺骗功能,jmeter遇到类似需求怎样实现呢? 环境:windows7,jdk1.8,jmeter3.1 使用IP欺骗功能前提是本地有多个可用IP, ...

  4. 测试工具——JMeter

    本学期新学的课程,软件测试,上机的实验用到了C++Test,QTP,还有JMeter.今天针对JMeter做一次总结,方便以后用到,知道步骤会更加方便. 首先,对Jmeter进行一个大致的了解,包括对 ...

  5. 移动端web解决方案

    范畴 移动端web浏览器.至少需要适配的,UC,QQ,各手机内置浏览器,chrome,safari. 是不是觉得和PC端差不多?错了!每款同一版本ios的内置浏览器一样.但每款同一版本android的 ...

  6. Android6.0-运行时权限处理

    为什么需要有运行时权限? 大家都知道在Android6.0之前,权限在应用安装过程中只询问一次,以列表的形式展现给用户,如果点击取消(即不认可应用所申请的权限),则会取消应用的安装.而用户出于安装应用 ...

  7. shell脚本中$参数的介绍

    $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表.如&quo ...

  8. 奇妙的 CSS shapes(CSS图形)

    CSS 发展到今天已经越来越强大了.其语法的日新月异,让很多以前完成不了的事情,现在可以非常轻松的做到.今天就向大家介绍几个比较新的强大的 CSS 功能: clip-path shape-outsid ...

  9. javaWEB之Servlet

    Servlet 1. 什么是Servlet  * Servlet是JavaWeb三大组件之一(Servlet.Filter.Listener)  * Servlet是用来处理客户端请求的动态资源  * ...

  10. javascript痛点之四this的指向问题

    先看以下例子 1.我们直接调用this看看指向的是谁 alert(this);//指向window 2.在函数中直接调用看看指向的是谁 function fn(){ alert(this); } fn ...