[LeetCode] Fraction to Recurring Decimal

简介: Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur.

Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur. So we need to maintain the remainders we have seen. Once we see a repeated remainder, we know that we have reached the end of the recurring parts and should enclose it with a ). However, we still need to insert the ( to the correct position. So we maintain a mapping from each remainder to the position of the corresponding quotient digit of it in the recurring parts. Then we use this mapping to retrieve the starting position of the recurring parts.

Now we have solved the trickiest part of this problem.

There are some remaining problems to solve to achieve a bug-free solution.

  1. Pay attention to the sign of the result;
  2. Handle cases that may cause overflow like numerator = -2147483648, denominator = -1appropriately by using long long;
  3. Handle all the cases of (1) no fractional part; (2) fractional part does not recur; and (3) fractional part recurs respectively.

To handle problem 3, we divide the division process into the integral part and the fractional part. For the fractional part, if it does not recur, then the remainder will become 0 at some point and we could return. If it does recur, the method metioned in the first paragraph has already handled it.

Taking all these into considerations, we have the following code. Note that I implement anint2str function, which may be replaced by the system to_string if you like.

 1 class Solution {
 2 public:
 3     string fractionToDecimal(int numerator, int denominator) {
 4         if (!numerator) return "0"; 
 5         string res;
 6         if (numerator < 0 ^ denominator < 0) res += '-';
 7         long long numer = numerator < 0 ? (long long)numerator * (-1) : (long long)numerator;
 8         long long denom = denominator < 0 ? (long long)denominator * (-1) : (long long)denominator;
 9         long long integral = numer / denom;
10         res += int2str(integral);
11         long long rmd = numer - integral * denom;
12         if (!rmd) return res;
13         res += '.';
14         rmd *= 10;
15         map<long long, long long> mp;
16         while (rmd) {
17             long long quotient = rmd / denom;
18             if (mp.find(rmd) != mp.end()) {
19                 res.insert(mp[rmd], 1, '(');
20                 res += ')';
21                 break;
22             }
23             mp[rmd] = res.size();
24             res += int2str(quotient);
25             rmd = (rmd - denom * quotient) * 10;
26         }
27         return res;
28     }
29 private:
30     string int2str(long long num) {
31         string str;
32         while (num) {
33             int digit = num % 10;
34             str += digit + '0';
35             num /= 10;
36         }
37         if (str.empty()) return "0";
38         reverse(str.begin(), str.end());
39         return str; 
40     }
41 };

 

目录
相关文章
|
网络架构
LeetCode 166 Fraction to Recurring Decimal (从分数到循环小数)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51726454 翻译 给定两个整数,用于表示一个分数的分子和分母,以字符串格式返回这个分数。
1106 0
|
机器学习/深度学习
[LeetCode] Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parenthes
1153 0
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
57 6
|
3月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
51 4
|
3月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
114 2
|
22天前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
17 1
|
2月前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
3月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
58 7