Fraction to Recurring Decimal


  • 0

    Click here to see the full article post


  • 0
    J

    You can avoid using a map. You don't need all reminders to test for a cycle, you need only the first reminder of the cycle. For example, if you do 1 / 3 result is 0.(3). Now the tricky part - how to figure out which reminder is first in the cycle, because you can have 1 / 6 = 0.1(6), 1 is outside of the cycle. Think that numerator = 10^x * 2^x * 5^z * rest, where x, y, z can be 0. To get the first reminder of the cycle you need to skip x+y+z reminders. It works. This way you skip using HashMap because using HashMap is MAGNITUDES slower than integer operations, this code spends 90% if not more on HashMap calls.


  • 0
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.