题目如下:

解题思路:本题考察的是分数的加减法。小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可。所以,本题只要按+,-两个符号分割输入字符串,就可以得到所有的分数列表,做加减操作即可。考虑到第一个分数是负数的情况,我在代码中加入了一个判断,如果表达式第一个字符是负数,给表达式加上'0/1'的前缀,既不影响结果,又简化了代码。

代码如下:

class Solution(object):
def fractionAddition(self, expression):
"""
:type expression: str
:rtype: str
"""
def gcd(a, b):
if a % b == 0:
return b
else:
return gcd(b, a % b)
def lcm(a,b):
return a * b / gcd(a,b)
if expression[0] == '-':
expression = '0/1' + expression
expression += '#'
operators = ['+','-']
operator = ''
next = ''
res = ''
for i in expression:
if i in operators or i == '#':
if operator == '':
operator = i
res = next
next = ''
else:
fl = res.split('/')
sl = next.split('/')
denominator = lcm(int(fl[1]),int(sl[1]))
if operator == '+':
numerator = int(fl[0]) * denominator/int(fl[1]) + int(sl[0]) * denominator/int(sl[1])
else:
numerator = int(fl[0]) * denominator / int(fl[1]) - int(sl[0]) * denominator / int(sl[1])
res = str(numerator) + '/' + str(denominator)
next = ''
operator = i
else:
next += i
rl = res.split('/')
g = gcd(int(rl[0]),int(rl[1])) res = str(int(rl[0])/g) + '/' + str(int(rl[1])/g)
return res

【leetcode】592. Fraction Addition and Subtraction的更多相关文章

  1. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

  2. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  3. [LeetCode] 592. Fraction Addition and Subtraction 分数加减法

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

  4. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  5. LC 592. Fraction Addition and Subtraction

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

  6. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  7. 【LeetCode】598. Range Addition II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【leetcode】598. Range Addition II

    You are given an m x n matrix M initialized with all 0's and an array of operations ops, where ops[i ...

  9. 【LeetCode】370. Range Addition 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

随机推荐

  1. [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:lovemu' did not find a matching property.

    [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.js ...

  2. 如何安全修改cocoapods上的第三方代码

    其实搞java的都知道maven管理,解决第三方代码修改的办法就是架一个服务器,把这些修改的库放在这里,然后再maven里配置哪些需要用本地仓库的.其实cocoapods也可以做本地包管理: 大致方法 ...

  3. vue路由vue-router的安装和使用

    1.安装,如果你没有在创建项目时候选择的情况下  cnpm install vue-router 2.使用    假设App为根组件,两个自定义组件home及list main.js里操作 impor ...

  4. php面试专题---22、网站优化 总结

    php面试专题---22.网站优化 总结 一.总结 一句话总结: 主要从前端.后端.数据库.资源四个方面开始发散 前端浏览器缓存和数据压缩前端优化(减少HTTP请求次数) 资源流量优化(防盗链处理)C ...

  5. SHADER 用 step替代 if-else

    今天聊起这个问题,百度发现了这个优化方式: https://blog.csdn.net/liu_if_else/article/details/77455639

  6. php用户签到,领取红包

    <?php /** * create by jxkshu * Date 2017-09-28 * 用户签到领取红包 */ /** Redis set: key signed:user:mark ...

  7. 五一清北学堂培训之数据结构(Day 1&Day 2)

    Day 1 前置知识: 二进制 2.基本语法 3.时间复杂度 正片       1.枚举 洛谷P1046陶陶摘苹果  入门题没什么好说的 判断素数:从2枚举到sqrt(n),若出现n的因子,则n是合数 ...

  8. English-taxonomy

    域.界.门.纲.目.科.属.种 Domain, Kingdom, Phylum, Class, Order, Family, Genus, Species

  9. Hugo - 安装、设置及使用

    Hugo 官方主页:https://gohugo.io 待选主题: https://github.com/cdipaolo/gindoro https://github.com/oserz/hugo- ...

  10. Python程序打包为可执行文件exe

    Python程序打包为可执行文件exe,pyinstaller应用 山重水复疑无路,柳暗花明又一村. 本来是向老师提交一个python程序,因为第一次所以就很尴尬只把源码给老师了,应该是打包成一个可执 ...