题目如下:

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

    1.北京时间格式   to   unix时间格式 数据格式: 2017-11-17 08:28:13 2017-11-17 08:28:10 2017-11-17 08:27:51.343 2017- ...

  2. .NET Core:目录

    ylbtech-.NET Core:目录 1.返回顶部 1. https://dotnet.microsoft.com/ 2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部   ...

  3. 当主机ip变了修改gitlab的ip地址

    gitlab服务器IP地址更换后需要修改以下两个配置中的IP地址: /var/opt/gitlab/gitlab-rails/etc/gitlab.yml /etc/gitlab/gitlab.rb ...

  4. Microsoft Azure_Fabric

    目录 目录 前言 Microsoft Azure Microsoft Azure Fabric Controller 前言 WindowsAzure是相对于全球版Microsoft Azure而言的中 ...

  5. WPF属性之理解附加属性

    附加属性,顾名思义,和被附加的控件没有依赖关系,只是强行给目标控件挂上一个“属性值”,以便于操作之.就好比,你在学校是学生,那么就要听老师的管教,在公司是下属,就要服从老板的命令一样. 我们常见的附加 ...

  6. 16/7/14-MySQL-遇到的基本问题

    从一开始遇到的3534 ---------------------------------------------------------------------------------------- ...

  7. dubbo入门和springboot集成dubbo小例子

    从零开始搭建springboot-dubbo的例子 Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,以及 SOA 服务治理方案 一. Dubbo的简单介绍 1. ...

  8. 关于国内注册codepen。无法收到邮件问题的解决

    我刚刚使用的qq邮箱也无法收到.后来使用了@foxmail.com邮箱就可以了. 我记得以前注册国外的一些东西,使用qq邮箱也是无法收到. 你可以在qq邮箱里面注册一个英文邮箱.注册以后就是@foxm ...

  9. System的两常用个静态方法

    package cn.learn; /* System类在java.lang.System,和操作系统有关 1.currentTimeMillis直接调用,是一个返回为long型的静态方法 常用来计算 ...

  10. Synchronized 详解

    为了方便记忆,将锁做如下的分类 一.对象锁 包括方法锁(默认锁对象为this,当前实例对象)和同步代码块锁(自己指定锁对象) 1.代码块形式:手动指定锁定对象,也可是是this,也可以是自定义的锁 p ...