-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFraction.java
More file actions
60 lines (45 loc) · 1.69 KB
/
Fraction.java
File metadata and controls
60 lines (45 loc) · 1.69 KB
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
/**
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.
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 A, int B)
{
StringBuilder answer = new StringBuilder();
if((A<0 && B>0) || (A>0 && B<0))
{
answer.append("-");
}
long numerator = A;
long denominator = B;
numerator = Math.abs(numerator);
denominator = Math.abs(denominator);
long result = numerator/denominator;
answer.append(String.valueOf(result));
long remainder = numerator%denominator * 10;
if(remainder == 0) return answer.toString();
answer.append(".");
Map<Long, Integer> map = new HashMap<Long, Integer>();
//running the loop till the time, number's last digit
while(remainder != 0)
{
if(map.containsKey(remainder))
{
int beg = map.get(remainder);
String part1 = answer.substring(0, beg);
String part2 = answer.substring(beg, answer.length());
return part1 + "(" + part2 + ")";
}
map.put(remainder, answer.length());
result = remainder/denominator;
answer.append(String.valueOf(result));
remainder = (remainder%denominator) * 10;
}
return answer.toString();
}
}