[leetcode]166. Fraction to Recurring Decimal
来源:互联网 发布:长城宽带 端口转发 编辑:程序博客网 时间:2024/06/05 22:49
题目链接:https://leetcode.com/problems/fraction-to-recurring-decimal/description/
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.
- Pay attention to the sign of the result;
- Handle cases that may cause overflow like
numerator = -2147483648, denominator = -1
appropriately by usinglong long
; - 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.
class Solution {public: string fractionToDecimal(int numerator, int denominator) { if (!numerator) return "0"; string res; if(numerator<0 ^ denominator<0) res+='-'; long long numer=numerator<0?(long long)numerator*(-1):(long long)numerator; long long denom=denominator<0?(long long)denominator*(-1):(long long)denominator; long long integral=numer/denom; res+=to_string(integral); long long rmd=numer%denom; if(!rmd) return res; res+='.'; rmd*=10; unordered_map<long long,long long> mp; while(rmd) { long long quotient=rmd/denom; if(mp.find(rmd)!=mp.end()) { res.insert(mp[rmd],1,'('); res+=')'; break; } mp[rmd]=res.size(); res+=to_string(quotient); rmd=(rmd%denom)*10; } return res; }};
- 166. Fraction to Recurring Decimal Leetcode Python
- Leetcode-166.Fraction to Recurring Decimal
- [Leetcode]166. Fraction to Recurring Decimal @python
- [leetcode] 166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- [LeetCode]166. Fraction to Recurring Decimal
- LeetCode *** 166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode]problem 166. Fraction to Recurring Decimal
- leetcode-java-166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- [LeetCode] 166. Fraction to Recurring Decimal
- Leetcode-166. Fraction to Recurring Decimal
- leetcode 166. Fraction to Recurring Decimal
- Leetcode 166. Fraction to Recurring Decimal
- LeetCode 166. Fraction to Recurring Decimal
- Leetcode 166. Fraction to Recurring Decimal
- leetCode 166. Fraction to Recurring Decimal
- malloc函数两个使用实例
- 安卓隐藏键盘与点击空白区域隐藏键盘
- tensorflow教程学习三深入MNIST
- 走迷宫——DFS
- 玲珑杯round#19B 1149 Buildings
- [leetcode]166. Fraction to Recurring Decimal
- Errors reported here must be corrected before the service can be started.
- 复盘_7月(第5周工作复盘)
- mac下node安装提示command not found
- 解决问题的记录【一】TCP连接过多导致新连接创建失败
- 用数组模拟栈结构,实现分隔符"{}"、"()"、"[]"的左右匹配检查
- android kotlin 一键配置 附anko
- 填制模具充程模拟Siemens.NX.11.0.Easy.Fill.Advanced.v5_20170720
- JAVA多线程核心技术 1.2.3 非线程安全 解析