leetcode-mid- math-166. Fraction to Recurring Decimal
mycode 73.92%
如何判断同号? 1)res = "-" if ((numerator>0) ^ (denominator>0)) else "" 2)如下
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
if denominator == 0: return None
if numerator == 0 :return ''
positive = '' if (numerator< 0) is (denominator < 0) else '-'
numerator = abs(numerator) if numerator < 0 else numerator
denominator = abs(denominator) if denominator < 0 else denominator
ans = ''
res = numerator // denominator
add = numerator % denominator
ans += str(res)
if add == 0:
return positive+ans
ans += '.'
repeat = []
while True:
res = add*10 // denominator
add = add*10 % denominator
if (res,add) in repeat:
pos = repeat.index((res,add))
for i,item in enumerate(repeat):
if i==pos:
ans += '('
ans += str(item[0])
ans += ')'
return positive+ans
else:
repeat.append((res,add))
if add == 0:
for i,item in enumerate(repeat):
ans += str(item[0])
return positive+ans
参考:
思路; 我记录了每次的商和余数,其实没必要啦,只需要记录余数即可,因为除数是不变的,余数重复出现了,自然商也就重复啦
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
""" def div(d,p):
x = d//p
y = d - (x*p )
return (x,y)
if numerator == 0: return ""
res = "-" if ((numerator>0) ^ (denominator>0)) else ""
numerator,denominator = abs(numerator),abs(denominator)
x ,y = div(numerator,denominator)
if y == 0:
return res + str(x)
x,y,denominator = abs(x),abs(y),abs(denominator) res += str(x) + "."
dic = {}
dic[y] = len(res)
while y:
y *= 10
x ,y = div(y , denominator)
res += str(x)
if y in dic:
pos = dic[y]
res = res[:pos] + '(' + res[pos:]+')'
break
else:
dic[y] = len(res)
return res
leetcode-mid- math-166. Fraction to Recurring Decimal的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal (Math)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- 166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如, 给出 分子 = 1, 分母 = 2,返回 "0.5". ...
随机推荐
- setTimeout定时器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring-data-jpa n+1问题
Spring-data-jpa的n+1问题 当我们使用JPA提供给我们的find方法时,如果查询出来的对象关联着另外10个对象,那么JPA将会发送1+10次查询(这个对象本身要查询一次,然后每个关联对 ...
- php 克隆 __clone
php 克隆 clone 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能会需要一个和A完全相同新对象B,并且此后对B任何改动都不会影响到A中的值, ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(3)|所有权Ownership]
今天我们来讲讲rust最难,也是最重要的概念: Ownership,Borrowing,Lifetimes 首先我们来看看:ownership(所有权) 我们来看看下面的代码: let a = [1, ...
- typedef int(init_fnc_t) (void)的理解
typedef int(init_fnc_t) (void); 这个就是一个取别名的过程. 我们通常情况下会如下使用 typedef :typedef int MyInt;MyInt a; 这个时候我 ...
- Java并发编程实战 第10章 避免活跃性危险
死锁 经典的死锁:哲学家进餐问题.5个哲学家 5个筷子 如果没有哲学家都占了一个筷子 互相等待筷子 陷入死锁 数据库设计系统中一般有死锁检测,通过在表示等待关系的有向图中搜索循环来实现. JVM没有死 ...
- 查看mysql数据库容量大小
第一种情况:查询所有数据库的总大小,方法如下: mysql> use information_schema; mysql> select concat(round(sum(DATA_LE ...
- canvas实现圆角图片 (处理原图是长方形或正方形)
/** * 生成图片的缩略图 * @param {[type]} img 图片(img)对象或地址 * @param {[type]} width 缩略图宽 * @param {[type]} hei ...
- 【ZJOJ5186】【NOIP2017提高组模拟6.30】tty's home
题目 分析 如果直接求方案数很麻烦. 但是,我们可以反过来做:先求出所有的方案数,在减去不包含的方案数. 由于所有的路径连在一起, 于是\(设f[i]表示以i为根的子树中,连接到i的方案数\) 则\( ...
- vue父组件异步获取动态数据传递给子组件获取不到值
原理: 在父组件中使用axios获取异步数据传给子组件,但是发现子组件在渲染的时候并没有数据,在created里面打印也是空的,结果发现一开始子组件绑定的数据是空的,在请求数据没有返回数据时,子组件就 ...