166 Fraction to Recurring Decimal
Problem:
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)”.
Thoughts:
One annoying point is that integer overflow, so the solution below is using long instead of integer.
Using a HashMap to store remainder so that we could know if a same remainder exists.
Solutions:
Last updated