8000 refactor 525 · devdcores/Leetcode@5cebad4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cebad4

Browse files
refactor 525
1 parent 603fb8b commit 5cebad4

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222
*/
2323
public class _525 {
2424

25-
//credit: https://leetcode.com/articles/contiguous-array/#approach-3-using-hashmap-accepted
26-
public int findMaxLength(int[] nums) {
27-
if (nums == null || nums.length == 0) {
28-
return 0;
29-
}
30-
int count = 0;
31-
int max = 0;
32-
Map<Integer, Integer> map = new HashMap();
33-
map.put(0, -1);//initialize the map, which means at index zero, the length of contiguous subarray is -1
34-
for (int i = 0; i < nums.length; i++) {
35-
count += nums[i] == 1 ? 1 : -1;
36-
if (map.containsKey(count)) {
37-
max = Math.max(i - map.get(count), max);
38-
} else {
39-
map.put(count, i);//only when the map does not have this key, we put it in the map, this avoids overwriting the map
40-
//also, it helps us keep the most leftside value in the map to help us compute the longest contigous array length
25+
public static class Solution1 {
26+
//credit: https://leetcode.com/articles/contiguous-array/#approach-3-using-hashmap-accepted
27+
public int findMaxLength(int[] nums) {
28+
if (nums == null || nums.length == 0) {
29+
return 0;
30+
}
31+
int count = 0;
32+
int max = 0;
33+
Map<Integer, Integer> map = new HashMap();
34+
map.put(0, -1);//initialize the map, which means at index zero, the length of contiguous subarray is -1
35+
for (int i = 0; i < nums.length; i++) {
36+
count += nums[i] == 1 ? 1 : -1;
37+
if (map.containsKey(count)) {
38+
max = Math.max(i - map.get(count), max);
39+
} else {
40+
map.put(count, i);//only when the map does not have this key, we put it in the map, this avoids overwriting the map
41+
//also, it helps us keep the most leftside value in the map to help us compute the longest contigous array length
42+
}
4143
}
44+
return max;
4245
}
43-
return max;
4446
}
45-
4647
}

src/test/java/com/fishercoder/_525Test.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.solutions._525;
34
import org.junit.Before;
45
import org.junit.BeforeClass;
56
import org.junit.Test;
67

78
import static junit.framework.Assert.assertEquals;
89

910
public class _525Test {
10-
private static com.fishercoder.solutions._525 test;
11+
private static _525.Solution1 solution1;
1112
private static int expected;
1213
private static int actual;
1314
private static int[] nums;
1415

1516
@BeforeClass
1617
public static void setup() {
17-
test = new com.fishercoder.solutions._525();
18+
solution1 = new _525.Solution1();
1819
}
1920

2021
@Before
@@ -25,23 +26,23 @@ public void setupForEachTest() {
2526
public void test1() {
2627
nums = new int[]{0, 1};
2728
expected = 2;
28-
actual = test.findMaxLength(nums);
29+
actual = solution1.findMaxLength(nums);
2930
assertEquals(expected, actual);
3031
}
3132

3233
@Test
3334
public void test2() {
3435
nums = new int[]{0, 1, 0};
3536
expected = 2;
36-
actual = test.findMaxLength(nums);
37+
actual = solution1.findMaxLength(nums);
3738
assertEquals(expected, actual);
3839
}
3940

4041
@Test
4142
public void test3() {
4243
nums = new int[]{0, 0, 1, 0, 0, 0, 1, 1};
4344
expected = 6;
44-
actual = test.findMaxLength(nums);
45+
actual = solution1.findMaxLength(nums);
4546
assertEquals(expected, actual);
4647
}
4748

0 commit comments

Comments
 (0)
0