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". ...
随机推荐
- Jquery复习(四)之text()、html()、val()
三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回 ...
- centos安装mysql以及授权登录用户
CentOS第一次安装MySQL的完整步骤 目录 1.官方安装文档 2.下载 Mysql yum包 3.安转软件源 4.安装mysql服务端 5.首先启动mysql ...
- 将本地的java项目提交到github出错解决
1.我们新建一个了java项目后,需要提交到github进行版本控制 2.如果此时github中的仓库不为空,我们在本地使用git push提交时会报以下错误, ! [rejected] ...
- atxserver2-rethinkdb的一些基础操作
因为我只需要一些基础操作就好,所以在web端的 Data Explorer 操作,首先启动 rethinkdb.exe,然后登陆网页端, 下面是一些基础操作的语句, 一.创建表r.db(‘atxse ...
- 初探html-17 表单
HTML 表单和输入 HTML 表单用于收集不同类型的用户输入. 在线实例 <form action=""> First name: <input type=&q ...
- python-scp-上传文件到服务器
python中使用scp,将文件上传到服务器 def ssh_scp_put(ip, username, password, local_file, remote_path): "" ...
- centos7 安装部署zabbix客户端
1.下载安装zabbix-agent: # rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2. ...
- framebuffer测试程序
/* framebuffer简单测试程序 网上转载 很多次 的程序 :-) */ #include <stdio.h> #include <stdlib.h> #include ...
- Jmeter线程组间传递参数
现在做测试和以前不太一样了,以前只要站在一个用户的角度做端到端的UI测试就可以了,现在不会做接口测试,出去都不好意思和别人打招呼.那提到接口测试,就不得不提一下接口测试利器Jmeter,大家也都知道, ...
- C#使用反射机制获取类信息
1.用反射动态创建类实例,并调用其公有成员函数. //新建一个类库项目,增加一个GetSum方法. using System; namespace ClassLibrary1 { publi ...