当前位置:网站首页>166. fractions to decimals
166. fractions to decimals
2022-07-01 03:43:00 【Sun_ Sky_ Sea】
166. Fractions to decimals
Original title link :https://leetcode.cn/problems/fraction-to-recurring-decimal/
Given two integers , Molecules representing fractions respectively numerator Denominator denominator, With Returns a decimal as a string .
If the decimal part is a circular decimal , The loop is enclosed in brackets .
If there are multiple answers , Just go back Any one .
For all given inputs , Guarantee The length of the answer string is less than 104 .
Example 1:
Input :numerator = 1, denominator = 2
Output :“0.5”
Example 2:
Input :numerator = 2, denominator = 1
Output :“2”
Example 3:
Input :numerator = 4, denominator = 333
Output :“0.(012)”
Tips :
-231 <= numerator, denominator <= 231 - 1
denominator != 0
Their thinking :
First determine whether the denominator is 0, In judging the sign , Then determine whether the decimal number is circular . Use a dictionary to determine if decimals are circular ,key Is the remainder ,value Is the quotient of the remainder divided by the divisor , That is to return the corresponding index position of the number in the string .
Code implementation :
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
# The denominator is 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))
# Remainder is 0, To divide or divide
if b == 0:
return "".join(ans)
# There are decimals without division , Add a decimal point
ans.append(".")
# Use a dictionary to record where decimals begin
# key Is the remainder ,value The quotient of the remainder after division corresponds to ans Position in
loc = {
b: len(ans)}
# Deal with the remainder
while b:
# The remainder is not enough for division , Expand the remainder by one digit , That's multiplied by 10
b *= 10
# a It's business. ,b Is the remainder , Did a division to update the remainder
a, b = divmod(b, denominator)
ans.append(str(a))
# If the remainder appears in the dictionary , It means that the cycle begins to repeat
# Add brackets according to the meaning of the question
if b in loc:
ans.insert(loc[b], "(")
ans.append(")")
break
# Record the new remainder, and the quotient after division corresponds to ans Position in
loc[b] = len(ans)
return "".join(ans)
reference :
https://leetcode.cn/problems/fraction-to-recurring-decimal/solution/ji-lu-yu-shu-by-powcai/
边栏推荐
- torch. histc
- Explain spark operation mode in detail (local+standalone+yarn)
- Feature Pyramid Networks for Object Detection论文理解
- 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
- Appium自动化测试基础 — APPium基本原理
- 【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
- Future of NTF and trends in 2022
- 【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- 389. 找不同
猜你喜欢

复习专栏之---消息队列

Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)

pytorch nn.AdaptiveAvgPool2d(1)

idea插件备份表

Download and installation configuration of cygwin

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

TEC: Knowledge Graph Embedding with Triple Context

静态库使用MFC和共享库使用MFC的区别

Avalanche problem and the use of sentinel

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
随机推荐
【伸手党福利】JSONObject转String保留空字段
idea插件备份表
Home online shopping project
Detailed explanation of ES6 deconstruction grammar
Develop industrial Internet with the technical advantages of small programs
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
谷粒学院微信扫码登录过程记录以及bug解决
171. Excel 表列序号
Thread data sharing and security -threadlocal
Jeecgboot output log, how to use @slf4j
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Appium自动化测试基础--补充:C/S架构和B/S架构说明
实现pow(x,n)函数
241. 为运算表达式设计优先级
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
5. [WebGIS practice] software operation - service release and permission management
Leetcode:829. Sum of continuous integers
Leetcode:829. 连续整数求和
在线公网安备案保姆级教程【伸手党福利】
Feign remote call and getaway gateway