Skip to content

Commit b5c7fb9

Browse files
refactor 548
1 parent 7258498 commit b5c7fb9

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,34 @@
2727
*/
2828
public class _548 {
2929

30-
public boolean splitArray(int[] nums) {
31-
int len = nums.length;
32-
if (len < 7) {
33-
return false;
34-
}
35-
int[] sum = new int[len];
36-
sum[0] = nums[0];
37-
for (int i = 1; i < len; i++) {
38-
sum[i] = sum[i - 1] + nums[i];
39-
}
40-
for (int j = 3; j < len - 3; j++) {
41-
Set<Integer> set = new HashSet<>();
42-
for (int i = 1; i < j - 1; i++) {
43-
if (sum[i - 1] == sum[j - 1] - sum[i]) {
44-
/**this is sum(0, i-1) and sum(i+1, j-1)*/
45-
set.add(sum[i - 1]);
46-
}
30+
public static class Solution1 {
31+
public boolean splitArray(int[] nums) {
32+
int len = nums.length;
33+
if (len < 7) {
34+
return false;
4735
}
48-
for (int k = j + 2; k < len - 1; k++) {
49-
if (sum[k - 1] - sum[j] == sum[len - 1] - sum[k] && set.contains(sum[k - 1] - sum[j])) {
50-
/**this is sum(j+1, k-1) and sum(k+1, len-1)*/
51-
return true;
36+
int[] sum = new int[len];
37+
sum[0] = nums[0];
38+
for (int i = 1; i < len; i++) {
39+
sum[i] = sum[i - 1] + nums[i];
40+
}
41+
for (int j = 3; j < len - 3; j++) {
42+
Set<Integer> set = new HashSet<>();
43+
for (int i = 1; i < j - 1; i++) {
44+
if (sum[i - 1] == sum[j - 1] - sum[i]) {
45+
/**this is sum(0, i-1) and sum(i+1, j-1)*/
46+
set.add(sum[i - 1]);
47+
}
48+
}
49+
for (int k = j + 2; k < len - 1; k++) {
50+
if (sum[k - 1] - sum[j] == sum[len - 1] - sum[k] && set.contains(sum[k - 1] - sum[j])) {
51+
/**this is sum(j+1, k-1) and sum(k+1, len-1)*/
52+
return true;
53+
}
5254
}
5355
}
56+
return false;
5457
}
55-
return false;
5658
}
5759

5860
}

src/test/java/com/fishercoder/_548Test.java

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

1010
public class _548Test {
11-
private static _548 test;
11+
private static _548.Solution1 test;
1212
private static boolean expected;
1313
private static boolean actual;
1414
private static int[] nums;
1515

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

2121
@Before

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