-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
32 lines (32 loc) · 947 Bytes
/
Solution.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
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0) {
return "0";
}
StringBuilder sb = new StringBuilder();
boolean neg = (numerator > 0) ^ (denominator > 0);
sb.append(neg ? "-" : "");
long num = Math.abs((long) numerator);
long d = Math.abs((long) denominator);
sb.append(num / d);
num %= d;
if (num == 0) {
return sb.toString();
}
sb.append(".");
Map<Long, Integer> mp = new HashMap<>();
while (num != 0) {
mp.put(num, sb.length());
num *= 10;
sb.append(num / d);
num %= d;
if (mp.containsKey(num)) {
int idx = mp.get(num);
sb.insert(idx, "(");
sb.append(")");
break;
}
}
return sb.toString();
}
}