题目如下:

解题思路:本题考察的是分数的加减法。小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可。所以,本题只要按+,-两个符号分割输入字符串,就可以得到所有的分数列表,做加减操作即可。考虑到第一个分数是负数的情况,我在代码中加入了一个判断,如果表达式第一个字符是负数,给表达式加上'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. app中使用

    KeepLive.startWork(this, KeepLive.RunMode.ROGUE, ForegroundNotification("Title", "mes ...

  2. 牛客:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数(数论+贪心)

    https://ac.nowcoder.com/acm/contest/907/B t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 分析: 根据约数和定理:对于一个大于1正整数 ...

  3. logback-spring.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false ...

  4. ERROR 1292 (22007): Truncated incorrect DOUBLE value: 'asfsda1'

    mysql> UPDATE financial_sales_order SET ASSIGN_TIME = '2018-05-02 00:00:00' where CUSTOMER_ID=354 ...

  5. 安装依赖的时候,报错npm WARN checkPermissions

    解决办法1 . 删除node_modules文件夹,重新安装依赖. 解决办法2 . 统一使用同一个npm安装依赖 . 原因:有的依赖包是用npm安装的,有的依赖包是用cnpm安装的.

  6. AppiumLibrary库倒入后显示红色,日志报错:ImportError: cannot import name 'InvalidArgumentException'

    AppiumLibrary安装后,robotframe worke 倒入后一直显示红色,查看日志报错:ImportError: cannot import name 'InvalidArgumentE ...

  7. HDU 2063 过山车 (匈牙利算法)

    题目链接:HDU 2063 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩 ...

  8. poj1065Wooden Sticks(dp——最长递减数列)

    Description There is a pile of n wooden sticks. The length and weight of each stick are known in adv ...

  9. python的包

    1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警觉:这是关于包才有的导入语法 2. 包是目录级的(文件夹级),文件夹是用 ...

  10. CentOS下编译Lua使得其支持动态链接

    在Linux下编译Lua时,我一般都是使用的make generic,这样编译没有什么问题,运行lua的程序也都OK,但是,这样在加载外部的C动态 链接库,却总是报下面的错误 dynamic libr ...