leetcode 166: Fraction to Recurring Decimal - 西施豆腐渣 - 博客频道 - CSDN.NET
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)".
[分析]
关键是确定发生循环的位置, 使用map存所有余数, 当余数已经出现过时, 即发生了循环.
[注意]
Overflow, 符号的处理, 分开两个函数, 使代码更容易读
Read full article from leetcode 166: Fraction to Recurring Decimal - 西施豆腐渣 - 博客频道 - CSDN.NET
No comments:
Post a Comment