@@ -0,0 +1,1643 @@
+/***%0A * Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.%0A *%0A * If the fractional part is repeating, enclose the repeating part in parentheses.%0A *%0A * For example,%0A *%0A * Given numerator = 1, denominator = 2, return %220.5%22.%0A * Given numerator = 2, denominator = 1, return %222%22.%0A * Given numerator = 2, denominator = 3, return %220.(6)%22.%0A */%0Apublic class Solution %7B%0A    public String fractionToDecimal(int numerator, int denominator) %7B%0A        if (denominator == 0)   return %22NaN%22;%0A        if (numerator == 0) return %220%22;%0A        StringBuilder ret = new StringBuilder();%0A        Long n = new Long(numerator);%0A        Long d = new Long(denominator);%0A        // Negative or positive%0A        if (n * d %3C 0)  ret.append('-');%0A        n = Math.abs(n);%0A        d = Math.abs(d);%0A        ret.append(Long.toString(n / d));%0A        // result is integer or float%0A        if (n %25 d == 0) return ret.toString();%0A        else%0A            ret.append('.');%0A        // Dealing with float part, store in HashMap%0A        // Key is the remainder, value is the start position of possible repeating numbers%0A        // for int, the repeating digits occurs uniquely.%0A        HashMap%3CLong, Integer%3E map = new HashMap%3CLong, Integer%3E();%0A        Long r = n %25 d;%0A        while (r %3E 0) %7B%0A            if (map.containsKey(r)) %7B%0A                ret.insert(map.get(r), %22(%22);%0A                ret.append(')');%0A                break;%0A            %7D%0A            map.put(r, ret.length());%0A            r *= 10;%0A            ret.append(Long.toString(r / d));%0A            r %25= d;%0A        %7D%0A        return ret.toString();%0A    %7D%0A%7D%0A

add FractiontoRecurringDecimal.java