Skip to content

Commit b814568

Browse files
refactor 238
1 parent 72a5f07 commit b814568

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515

1616
public class _238 {
1717

18-
/**Very straightforward idea: iterate through the array twice:
19-
* first time: get res[i] = res[i-1]*nums[i-1]
20-
* second time: have a variable called right, which means all the numbers product to its right, then do
21-
* res[i] *= right;
22-
* right *= nums[i];
23-
* that's it.
24-
25-
* This could be very well illustrated with this example: [1,2,3,4]*/
26-
public int[] productExceptSelf(int[] nums) {
27-
int n = nums.length;
28-
int[] result = new int[n];
29-
result[0] = 1;
30-
for (int i = 1; i < n; i++) {
31-
result[i] = result[i - 1] * nums[i - 1];
32-
}
33-
int right = 1;
34-
for (int i = n - 1; i >= 0; i--) {
35-
result[i] *= right;
36-
right *= nums[i];
18+
public static class Solution1 {
19+
/**
20+
* Very straightforward idea: iterate through the array twice:
21+
* first time: get res[i] = res[i-1]*nums[i-1]
22+
* second time: have a variable called right, which means all the numbers product to its right, then do
23+
* res[i] *= right;
24+
* right *= nums[i];
25+
* that's it.
26+
* <p>
27+
* This could be very well illustrated with this example: [1,2,3,4]
28+
*/
29+
public int[] productExceptSelf(int[] nums) {
30+
int n = nums.length;
31+
int[] result = new int[n];
32+
result[0] = 1;
33+
for (int i = 1; i < n; i++) {
34+
result[i] = result[i - 1] * nums[i - 1];
35+
}
36+
int right = 1;
37+
for (int i = n - 1; i >= 0; i--) {
38+
result[i] *= right;
39+
right *= nums[i];
40+
}
41+
return result;
3742
}
38-
return result;
3943
}
4044
}

src/test/java/com/fishercoder/_238Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import static org.junit.Assert.assertArrayEquals;
99

1010
public class _238Test {
11-
private static _238 test;
11+
private static _238.Solution1 solution1;
1212
private static int[] expected;
1313
private static int[] actual;
1414
private static int[] nums;
1515

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

2121
@Before
@@ -28,23 +28,23 @@ public void setupForEachTest() {
2828
public void test1() {
2929
nums = new int[]{0, 0};
3030
expected = new int[]{0, 0};
31-
actual = test.productExceptSelf(nums);
31+
actual = solution1.productExceptSelf(nums);
3232
assertArrayEquals(expected, actual);
3333
}
3434

3535
@Test
3636
public void test2() {
3737
nums = new int[]{1, 0};
3838
expected = new int[]{0, 1};
39-
actual = test.productExceptSelf(nums);
39+
actual = solution1.productExceptSelf(nums);
4040
assertArrayEquals(expected, actual);
4141
}
4242

4343
@Test
4444
public void test3() {
4545
nums = new int[]{1, 2, 3, 4};
4646
expected = new int[]{24, 12, 8, 6};
47-
actual = test.productExceptSelf(nums);
47+
actual = solution1.productExceptSelf(nums);
4848
assertArrayEquals(expected, actual);
4949
}
5050
}

0 commit comments

Comments
 (0)