-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathFractionToRecurringDecimal166.java
103 lines (80 loc) · 2.9 KB
/
FractionToRecurringDecimal166.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* 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 FractionToRecurringDecimal166 {
public String fractionToDecimal(int numerator, int denominator) {
boolean sameSign = (numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0);
long nume = Math.abs((long)numerator);
long deno = Math.abs((long)denominator);
Long ones = nume / deno;
List<Long> decimals = new ArrayList<>();
long left = nume % deno;
Map<Long, Integer> map = new HashMap<>();
left *= 10;
int pos = 0;
int repeat = -1;
while (true) {
if (left == 0) break;
if (map.containsKey(left)) {
repeat = map.get(left);
break;
}
long newOne = Math.abs(left / deno);
long newLeft = left % deno;
decimals.add(newOne);
map.put(left, pos);
left = newLeft*10;
pos++;
}
String pre = (sameSign || ones <= 0L) ? ones.toString() : ("-" + ones.toString());
if (decimals.size() == 0) return pre;
StringBuilder sb = new StringBuilder();
for (int i=0; i<decimals.size(); i++) {
String curr = decimals.get(i).toString();
String newD = (repeat != i) ? curr : ("(" + curr);
sb.append(newD);
}
if (repeat != -1) sb.append(")");
return String.format("%s.%s", (sameSign) ? ones.toString() : ("-" + ones.toString()), sb);
}
public String fractionToDecimal2(int numerator, int denominator) {
if (numerator == 0) {
return "0";
}
StringBuilder fraction = new StringBuilder();
if (numerator < 0 ^ denominator < 0) {
fraction.append("-");
}
long num = Math.abs(Long.valueOf(numerator));
long denom = Math.abs(Long.valueOf(denominator));
fraction.append(num / denom);
long remainder = num % denom;
if (remainder == 0) {
return fraction.toString();
}
fraction.append(".");
HashMap<Long, Integer> map = new HashMap<>();
while (remainder != 0) {
if (map.containsKey(remainder)) {
fraction.insert(map.get(remainder), "(");
fraction.append(")");
break;
}
map.put(remainder, fraction.length());
remainder *= 10;
fraction.append(remainder / denom);
remainder = remainder % denom;
}
return fraction.toString();
}
}