【leetcode】592. Fraction Addition and Subtraction
题目如下:

解题思路:本题考察的是分数的加减法。小学时候就学过,分数的加减法是先求两个分母的最小公倍数,然后分子分别乘以最小公倍数与自己分母的商,相加后约分即可。所以,本题只要按+,-两个符号分割输入字符串,就可以得到所有的分数列表,做加减操作即可。考虑到第一个分数是负数的情况,我在代码中加入了一个判断,如果表达式第一个字符是负数,给表达式加上'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的更多相关文章
- 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)
[LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- [LeetCode] 592. Fraction Addition and Subtraction 分数加减法
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- LC 592. Fraction Addition and Subtraction
Given a string representing an expression of fraction addition and subtraction, you need to return t ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【LeetCode】598. Range Addition II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【LeetCode】370. Range Addition 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
随机推荐
- 基于MyBatis实现Dao理论
基于MyBatis实现Dao理论 推荐使用xml提供sql 实现接口推荐使用Mapper自动实现DAO接口,让我们更关注sql书写本身
- 【前端技术】一篇文章搞掂:uni-app
语法 //列表遍历,遍历数组,第一个参数为数组中元素,可以给第二个参数作为索引值 <view v-for="(item, itemIndex) in card" :key=& ...
- 10 Advanced Bing Search Tricks You Should Know
Exclude Websites From Bing Search: wikipedia -wikipedia.org Excluding Keywords From Bing Search: fac ...
- linux中awk 详解
一.awk简介 awk是一个非常好用的数据处理工具,相对于sed常常作用于一整个行的处理,awk则比较倾向于一行当中分成数个[字段]处理,因此,awk相当适合处理小型的数据数据处理.awk是一种报表生 ...
- 搭建maven本地仓库
1. 需先配置java环境. 2. 下载nexus. https://www.sonatype.com/download-nexus-repo-oss?submissionGuid=a015a3db- ...
- MySQL 查询语句--------------进阶6:连接查询
#进阶6:连接查询 /* 含义:多个表格连接,当查询的字段来自于多个表时候,就会用到连接查询 我觉得这里类似于excel中的vlookup函数 笛卡尔乘积现象:表1有m行,表2有n行,结果有m*n行 ...
- URL的‘#’号
转载自:传送门 去年9月,twitter改版. 一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为 http://twitter.com/username 改 ...
- onblur和onkeyup事件
onblur:事件会在对象失去焦点时发生 提示:onblur 相反事件为onfocus事件 . onkeyup: 事件会在键盘按键被松开时发生. 提示:与onkeyup 事件相关的事件发生次序: on ...
- 09 (H5*) JS第7天 原型
目录 1:创建对象的3中方式 2:工厂模式创建实例对象 3: 实例对象和构造函数的关系 4:构造函数创建对象带来的问题--原型 5:原型中创建方法 6:构造函数.原型对象.实例对象的关系 7:原型对 ...
- ExcelVBA 操作putty
Private Sub login(ip As String, userName As String, password As String) Dim TaskID As Long '设置进程id p ...