forked from xingzhougmu/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrac2Decimal
60 lines (56 loc) · 1.52 KB
/
Frac2Decimal
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
private static String Frac2Decimal(int nume, int denom){
char[] result = new char[1000];
int sign = (nume*denom>=0) ? 1: -1; % get the sign of the result
int remainder = 0;
int quotient = 0;
int position = 0;
int duplicate = 0;
if (sign < 0)
result[position++] = '-';
if (nume>=denom){ % if has non-fractional part
quotient = nume / denom;
remainder = nume % denom;
if (!hash.containsKey(remainder)){
hash.put(remainder, position);
result[position++] = (char) quotient;
result[position++] = '.';
}
nume = remainder;
}else{
result[position++] = '0';
result[position++] = '.';
}
while (true){
nume *=10;
quotient = nume / denom;
remainder = nume % denom;
nume = remainder;
if (remainder == 0){
result[position++] = (char) ('0' + quotient);
duplicate = -1;
break;
}
if (!hash.containsKey(remainder)){ % push remainder to hash table
hash.put(remainder, position);
result[position++] = (char) ('0' + quotient);
}
else {
duplicate = hash.get(remainder);
break;
}
}
if (duplicate > 0){
result[++position] = ')';
for (int i=position-1; i>duplicate; i--)
result[i] = result[i-1];
result[duplicate] = '(';
result[++position] = '\0';
} else{
result[position++] = '(';
result[position++] = '0';
result[position++] = ')';
result[position++] = '\0';
}
return String.copyValueOf(result);
}
private static Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();