Description
https://leetcode.com/problems/fraction-to-recurring-decimal/description/
Solution
Pay attention to the fact that the pair of quote plus redundant in each divide iteration is the feature to judge whether the loop happened. Based on which, We ssing a list to record the order of the pair list, and maintain a set to judge whether the pair is occupied in each iteration. Once it was in the set, we stop it. Then we traverse the pair list to find the loop part and the unloop part.
1 | class Pair { |
Attention
- In java, you can use the class as the key word in the map(set), but to make them work functionally, you need some extra effort——override the
hashCode
andequals
methods in the class. - The corner case of big int can reduce to some awkward situation, so my suggestion is convert the parmeter to long type.