-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction to Recurring Decimal.java
46 lines (44 loc) · 1.57 KB
/
Fraction to Recurring Decimal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*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)".*/
public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (denominator == 0)
return "NaN";
if (numerator == 0)
return "0";
StringBuilder result = new StringBuilder();
Long n = new Long(numerator);
Long d = new Long(denominator);
// negative or positive
if (n*d < 0)
result.append("-");
n = Math.abs(n);
d = Math.abs(d);
result.append(Long.toString(n / d));
// result is integer or float
if (n % d == 0)
return result.toString();
else
result.append(".");
// deal with the float part
// key is the remainder, value is the start positon of possible repeat numbers
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
Long r = n % d;
while (r > 0) {
if (map.containsKey(r)) {
result.insert(map.get(r), "(");
result.append(")");
break;
}
map.put(r, result.length());
r *= 10;
result.append(Long.toString(r / d));
r %= d;
}
return result.toString();
}
}