Leetcode: Fraction to Recurring Decimal
来源:互联网 发布:熊猫手机直播软件下载 编辑:程序博客网 时间:2024/05/21 19:40
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 parentheses.
For example,
- Given numerator = 1, denominator = 2, return "0.5".
- Given numerator = 2, denominator = 1, return "2".
- Given numerator = 2, denominator = 3, return "0.(6)"
0.16 6 ) 1.00 0 1 0 <-- Remainder=1, mark 1 as seen at position=0. - 6 40 <-- Remainder=4, mark 4 as seen at position=1. - 36 4 <-- Remainder=4 was seen before at position=1, so the fractional part which is 16 starts repeating at position=1 => 1(6).
The key insight here is to notice that once the remainder starts repeating, so does the divided result.
You will need a hash table that maps from the remainder to its position of the fractional part. Once you found a repeating remainder, you may enclose the reoccurring fractional part with parentheses by consulting the position from the table.
The remainder could be zero while doing the division. That means there is no repeating fractional part and you should stop right away.
Just like the question Divide Two Integers, be wary of edge case such as negative fractions and nasty extreme case such as -2147483648 / -1
.
以上思路来自leetcode的solution: https://leetcode.com/problems/fraction-to-recurring-decimal/solution/
至于如何解决corner cases, 我使用long int来存储分子分母,解决corner cases,避免大量讨论。
代码:
class Solution {public: string fractionToDecimal(int numerator, int denominator) { if(numerator == 0) return "0"; string ret; if((numerator < 0 ) ^ (denominator < 0)) ret += "-"; long int n = abs((long int)numerator); long int d = abs((long int)denominator); ret += to_string(n / d); long int r = n % d; if(r == 0) return ret; else ret += '.'; unordered_map<int, int> map; while(r) { if(map.find(r) != map.end()) { ret.insert(map[r], 1, '('); ret += ')'; break; } map[r] = ret.size(); r *= 10; ret += to_string(r / d); r = r % d; } return ret; }};
- [leetcode] Fraction to Recurring Decimal
- LeetCode Fraction to Recurring Decimal
- Fraction to Recurring Decimal leetcode
- leetcode:Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [leetCode] Fraction to Recurring Decimal
- LeetCode:Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- leetcode:Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [leetcode]Fraction to Recurring Decimal
- LeetCode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- leetcode: Fraction to Recurring Decimal
- android Jobscheduler的用法
- (2.2.1)设计模式一览
- 信号量-内核信号量、POSIX信号量、system V信号量
- Linxu内核中断
- 二级 长度为7的字符串除首位外其余五个字符按ascii码进行排序
- Leetcode: Fraction to Recurring Decimal
- 黑马程序员——Java语言基础
- JVM什么时候退出
- C#探秘系列(一)
- 最近二天写了一点水题的经验教训
- 列的操作(二)
- csdn编程练习之回文距离
- enterprise architect (EA) 源码生成UML类图,帮助理解项目工程
- 计算1~100中9出现的次数