Skip to content

Commit 3696a2b

Browse files
refactor 166
1 parent fc441dc commit 3696a2b

File tree

2 files changed

+80
-81
lines changed

2 files changed

+80
-81
lines changed

src/main/java/com/fishercoder/solutions/_166.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,41 @@
1818
1919
*/
2020
public class _166 {
21-
/**credit: https://discuss.leetcode.com/topic/33311/simple-and-short-solution-in-java*/
21+
public static class Solution1 {
22+
/** credit: https://discuss.leetcode.com/topic/33311/simple-and-short-solution-in-java */
2223
public String fractionToDecimal(int numerator, int denominator) {
23-
String sign = (numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0) ? "" : "-";
24-
if (numerator == 0) {
25-
return "0";
26-
}
27-
long num = Math.abs((long) numerator);
28-
long deno = Math.abs((long) denominator);
29-
StringBuilder stringBuilder = new StringBuilder();
30-
stringBuilder.append(sign);
31-
long integral = Math.abs(num / deno);
32-
stringBuilder.append(integral);
33-
if (numerator % denominator == 0) {
34-
return stringBuilder.toString();
35-
} else {
36-
stringBuilder.append(".");
37-
}
38-
long remainder = num % deno;
39-
40-
Map<Long, Integer> map = new HashMap<>();
41-
while (!map.containsKey(remainder)) {
42-
map.put(remainder, stringBuilder.length());
43-
long n = remainder * 10 / deno;
44-
remainder = remainder * 10 % deno;
45-
if (remainder != 0 || (remainder == 0 && !map.containsKey(remainder))) {
46-
stringBuilder.append(n);
47-
}
48-
}
49-
if (remainder != 0) {
50-
stringBuilder.insert(map.get(remainder), "(");
51-
stringBuilder.append(")");
52-
}
24+
String sign =
25+
(numerator >= 0 && denominator >= 0) || (numerator < 0 && denominator < 0) ? "" : "-";
26+
if (numerator == 0) {
27+
return "0";
28+
}
29+
long num = Math.abs((long) numerator);
30+
long deno = Math.abs((long) denominator);
31+
StringBuilder stringBuilder = new StringBuilder();
32+
stringBuilder.append(sign);
33+
long integral = Math.abs(num / deno);
34+
stringBuilder.append(integral);
35+
if (numerator % denominator == 0) {
5336
return stringBuilder.toString();
37+
} else {
38+
stringBuilder.append(".");
39+
}
40+
long remainder = num % deno;
41+
42+
Map<Long, Integer> map = new HashMap<>();
43+
while (!map.containsKey(remainder)) {
44+
map.put(remainder, stringBuilder.length());
45+
long n = remainder * 10 / deno;
46+
remainder = remainder * 10 % deno;
47+
if (remainder != 0 || (remainder == 0 && !map.containsKey(remainder))) {
48+
stringBuilder.append(n);
49+
}
50+
}
51+
if (remainder != 0) {
52+
stringBuilder.insert(map.get(remainder), "(");
53+
stringBuilder.append(")");
54+
}
55+
return stringBuilder.toString();
5456
}
55-
57+
}
5658
}

src/test/java/com/fishercoder/_166Test.java

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,51 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/28/17.
11-
*/
129
public class _166Test {
13-
private static _166 test;
14-
15-
@BeforeClass
16-
public static void setup() {
17-
test = new _166();
18-
}
19-
20-
@Test
21-
public void test1() {
22-
assertEquals("0.5", test.fractionToDecimal(1, 2));
23-
}
24-
25-
@Test
26-
public void test2() {
27-
assertEquals("2", test.fractionToDecimal(2, 1));
28-
}
29-
30-
@Test
31-
public void test3() {
32-
assertEquals("0.(6)", test.fractionToDecimal(2, 3));
33-
}
34-
35-
@Test
36-
public void test4() {
37-
assertEquals("-6.25", test.fractionToDecimal(-50, 8));
38-
}
39-
40-
@Test
41-
public void test5() {
42-
assertEquals("-0.58(3)", test.fractionToDecimal(7, -12));
43-
}
44-
45-
@Test
46-
public void test6() {
47-
assertEquals("0.0000000004656612873077392578125", test.fractionToDecimal(-1, -2147483648));
48-
}
49-
50-
@Test
51-
public void test7() {
52-
assertEquals("0", test.fractionToDecimal(0, -5));
53-
}
54-
55-
@Test
56-
public void test8() {
57-
assertEquals("-2147483648", test.fractionToDecimal(-2147483648, 1));
58-
}
10+
private static _166.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _166.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("0.5", solution1.fractionToDecimal(1, 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("2", solution1.fractionToDecimal(2, 1));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("0.(6)", solution1.fractionToDecimal(2, 3));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("-6.25", solution1.fractionToDecimal(-50, 8));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals("-0.58(3)", solution1.fractionToDecimal(7, -12));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals("0.0000000004656612873077392578125", solution1.fractionToDecimal(-1, -2147483648));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals("0", solution1.fractionToDecimal(0, -5));
50+
}
51+
52+
@Test
53+
public void test8() {
54+
assertEquals("-2147483648", solution1.fractionToDecimal(-2147483648, 1));
55+
}
5956
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy