当前位置:网站首页>166. 分数到小数
166. 分数到小数
2022-07-01 03:23:00 【Sun_Sky_Sea】
166. 分数到小数
原始题目链接:https://leetcode.cn/problems/fraction-to-recurring-decimal/
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数 。
如果小数部分为循环小数,则将循环的部分括在括号内。
如果存在多个答案,只需返回 任意一个 。
对于所有给定的输入,保证 答案字符串的长度小于 104 。
示例 1:
输入:numerator = 1, denominator = 2
输出:“0.5”
示例 2:
输入:numerator = 2, denominator = 1
输出:“2”
示例 3:
输入:numerator = 4, denominator = 333
输出:“0.(012)”
提示:
-231 <= numerator, denominator <= 231 - 1
denominator != 0
解题思路:
先判断分母是否为0,在判断正负号,然后判断小数是否循环。 使用一个字典来判断小数是否循环,key是余数,value是余数对除数相除的商,即要返回字符串中的数字的对应的索引位置。
代码实现:
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
# 分母为0
if numerator == 0:
return "0"
ans = []
if (numerator > 0) ^ (denominator > 0):
ans.append("-")
numerator, denominator = abs(numerator), abs(denominator)
a, b = divmod(numerator, denominator)
ans.append(str(a))
# 余数为0,表示整除
if b == 0:
return "".join(ans)
# 不整除即有小数,加个小数点
ans.append(".")
# 用一个字典记录小数开始的位置
# key是余数,value是余数做除法后的商对应在ans中的位置
loc = {
b: len(ans)}
# 处理余数
while b:
# 余数相对于除数位数不够做除法,将余数扩大一位,即乘以10
b *= 10
# a是商,b是余数,做了除法更新余数
a, b = divmod(b, denominator)
ans.append(str(a))
# 如果余数在字典中出现过了,表示开始重复循环了
# 依据题意加括号
if b in loc:
ans.insert(loc[b], "(")
ans.append(")")
break
# 记录新的余数做除法后的商对应在ans中的位置
loc[b] = len(ans)
return "".join(ans)
参考文献:
https://leetcode.cn/problems/fraction-to-recurring-decimal/solution/ji-lu-yu-shu-by-powcai/
边栏推荐
- LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
- Feign remote call and getaway gateway
- Jeecgboot output log, how to use @slf4j
- Complete knapsack problem
- Cygwin的下载和安装配置
- Ridge regression and lasso regression
- Valentine's Day is nothing.
- Use of comment keyword in database
- Take you through a circuit board, from design to production (dry goods)
- go实现命令行的工具cli
猜你喜欢

线程数据共享和安全 -ThreadLocal

Edlines: a real time line segment detector with a false detection control

FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)

Ridge regression and lasso regression

Thread data sharing and security -threadlocal

bootsrap中的栅格系统

File upload and download

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

5、【WebGIS实战】软件操作篇——服务发布及权限管理
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management
随机推荐
详解Spark运行模式(local+standalone+yarn)
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
Finally in promise
IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
ECMAScript 6.0
Feature Pyramid Networks for Object Detection论文理解
Valid brackets (force deduction 20)
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Feign remote call and getaway gateway
Unexpected token o in JSON at position 1 ,JSON解析问题
Develop industrial Internet with the technical advantages of small programs
Cygwin的下载和安装配置
leetcode 1818 绝对值,排序,二分法,最大值
The shell script uses two bars to receive external parameters
【快捷键】
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
How to achieve 0 error (s) and 0 warning (s) in keil5
岭回归和lasso回归
Pyramid scene parsing network [pspnet] thesis reading
Keil5中如何做到 0 Error(s), 0 Warning(s).