/
Solution.java
executable file
·91 lines (67 loc) · 2.55 KB
/
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
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
import java.util.*;
public class Solution {
public String fractionToDecimal(long numerator, long denominator) {
boolean neg = false;
if((numerator > 0 && denominator < 0) || (numerator < 0 && denominator > 0))
neg = true;
numerator = Math.abs(numerator);
denominator = Math.abs(denominator);
String res = "";
if(neg) res = "-";
if(denominator == 0) return "0";
if(numerator % denominator == 0){
return res + numerator/denominator;
}
res += numerator / denominator + ".";
numerator %= denominator;
Map<Long, Integer> dict = new HashMap<>();
dict.put(numerator, res.length());
while(numerator > 0){
numerator *= 10;
res += numerator / denominator;
numerator %= denominator;
if(dict.containsKey(numerator)) {
res = res.substring(0, dict.get(numerator)) + "("
+ res.substring(dict.get(numerator), res.length()) + ")";
break;
}
else
dict.put(numerator, res.length());
}
return res;
}
}
public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
String sign = "";
if(Math.signum(numerator) * Math.signum(denominator) < 0){
sign = "-";
}
// cheat ...
long _numerator = numerator;
long quotient = _numerator / denominator;
_numerator %= denominator;
_numerator *= 10;
final String intPart = "" + Math.abs(quotient);
Map<String, Integer> mod = new HashMap<String, Integer>();
int i = 0;
StringBuilder sb = new StringBuilder();
while(_numerator != 0){
quotient = Math.abs(_numerator / denominator);
_numerator %= denominator;
Integer start = mod.get(_numerator + "," + quotient);
if(start != null){
sb.insert(start, "(");
sb.append(")");
break;
}
sb.append(quotient);
mod.put(_numerator + "," + quotient, i);
_numerator *= 10;
i++;
}
String fractionalPart = sb.toString();
if(!"".equals(fractionalPart)) fractionalPart = "." + fractionalPart;
return sign + intPart + fractionalPart;
}
}