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. [笔记]我的Linux入门之路 - 03.Java环境搭建

    其实ubuntu是自带一个叫openJDK的东西的,是谷歌看Oracle不爽而搞的.不过呢...总感觉不太习惯,况且我既然都来Linux了,总是想折腾一把的. 首先先检查下有没有安装java.终端输入 ...

  2. PHPMailer 命令执行漏洞(CVE-2016-10033)分析(含通用POC)

    对比一下新老版本:https://github.com/PHPMailer/PHPMailer/compare/v5.2.17…master 其实答案呼之欲出了——和Roundcube的RCE类似,m ...

  3. 【JAVAWEB学习笔记】10_JDBC连接池&DBUtils

    使用连接池改造JDBC的工具类: 1.1.1          需求: 传统JDBC的操作,对连接的对象销毁不是特别好.每次创建和销毁连接都是需要花费时间.可以使用连接池优化的程序. * 在程序开始的 ...

  4. html学习笔记 - meta link

    <!DOCTYPE html> <html lang="en"> <head> <!-- 编码格式 --> <meta cha ...

  5. 计蒜客蓝桥杯模拟赛五J. 程序设计:放置守卫

    在一张 n 行 m 列的方格地图上放置一些守卫,每个守卫能守护上.左.右三个方向上相邻的方格和自己所在的方格.如下图,红色的方格放置守卫,绿色的方格为该守卫守护的区域. 现在要求在地图上放置若干个守卫 ...

  6. SimpleDateFormat使用和线程安全问题

    SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text -> date)和标准化. Simpl ...

  7. 【持续集成】GIT+jenkins+snoar——jenkins发布php、maven项目

    一.持续集成 1.1 什么是持续集成? continuous integration (CI),持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员,每天至少集成一次,也就意味着 ...

  8. Java微服务框架

    Java的微服务框架dobbo.spring boot.redkale.spring cloud 消息中间件RabbitMQ.Kafka.RocketMQ

  9. css3 linear-gradient渐变效果及兼容性处理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 天气正好,hello world!

    两个多月,稀里糊涂的回来了,内心很迷茫,回来一周了,明知道还需要有一大堆东西需要去学,但是却找不到之前学习的状态,在寝室,在实验室,看着自己一向不喜欢的电视剧,无目的的逛着淘宝,刷着头条和知乎,就这么 ...