8000 refactor 553 · venkim/Leetcode@1dbc03f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1dbc03f

Browse files
refactor 553
1 parent 7f54bda commit 1dbc03f

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,34 @@
3131
There is only one optimal division for each test case.
3232
*/
3333
public class _553 {
34+
public static class Solution1 {
35+
/**
36+
* Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java
37+
*/
38+
public String optimalDivision(int[] nums) {
39+
/**https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html:
40+
* StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
41+
* The String "[George:Sally:Fred]" may be constructed as follows:
42+
StringJoiner sj = new StringJoiner(":", "[", "]");
43+
sj.add("George").add("Sally").add("Fred");
44+
String desiredString = sj.toString();*/
3445

35-
/**Credit: https://github.com/lydxlx1/LeetCode/blob/master/src/_553.java*/
36-
public String optimalDivision(int[] nums) {
37-
/**https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html:
38-
* StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
39-
* The String "[George:Sally:Fred]" may be constructed as follows:
40-
StringJoiner sj = new StringJoiner(":", "[", "]");
41-
sj.add("George").add("Sally").add("Fred");
42-
String desiredString = sj.toString();*/
43-
44-
if (nums.length == 1) {
45-
return "" + nums[0];
46-
}
47-
if (nums.length == 2) {
48-
return nums[0] + "/" + nums[1];
49-
}
46+
if (nums.length == 1) {
47+
return "" + nums[0];
48+
}
49+
if (nums.length == 2) {
50+
return nums[0] + "/" + nums[1];
51+
}
5052

51-
/**Tricky one: the solution is fixed: always wrap the one from the second until the last.
52-
* Another important thing to note that such way could work is that:
53-
* the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/
54-
StringJoiner stringJoiner = new StringJoiner("/");
55-
for (int i = 1; i < nums.length; i++) {
56-
stringJoiner.add("" + nums[i]);
53+
/**Tricky one: the solution is fixed: always wrap the one from the second until the last.
54+
* Another important thing to note that such way could work is that:
55+
* the prerequisite is: Elements will be in range [2,1000], so no elements are smaller than 1.*/
56+
StringJoiner stringJoiner = new StringJoiner("/");
57+
for (int i = 1; i < nums.length; i++) {
58+
stringJoiner.add("" + nums[i]);
59+
}
60+
return String.format("%d/(%s)", nums[0], stringJoiner.toString());
5761
}
58-
return String.format("%d/(%s)", nums[0], stringJoiner.toString());
5962
}
6063

6164
}

src/test/java/com/fishercoder/_553Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
* Created by fishercoder on 5/25/17.
1111
*/
1212
public class _553Test {
13-
private static _553 test;
13+
private static _553.Solution1 solution1;
1414
private static int[] nums;
1515

1616
@BeforeClass
1717
public static void setup() {
18-
test = new _553();
18+
solution1 = new _553.Solution1();
1919
}
2020

2121
@Test
2222
public void test1() {
2323
nums = new int[]{1000, 100, 10, 2};
24-
assertEquals("1000/(100/10/2)", test.optimalDivision(nums));
24+
assertEquals("1000/(100/10/2)", solution1.optimalDivision(nums));
2525
}
2626

2727
@Test
2828
public void test2() {
2929
nums = new int[]{1000, 100, 40, 10, 2};
30-
assertEquals("1000/(100/40/10/2)", test.optimalDivision(nums));
30+
assertEquals("1000/(100/40/10/2)", solution1.optimalDivision(nums));
3131
}
3232
}

0 commit comments

Comments
 (0)
0