Skip to content

Commit 55e6e16

Browse files
refactor 228
1 parent 8cccb10 commit 55e6e16

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,30 @@
44
import java.util.List;
55

66
/**
7+
* 228. Summary Ranges
8+
*
79
* Given a sorted integer array without duplicates, return the summary of its ranges.
8-
9-
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
10+
*
11+
* For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
1012
*/
1113
public class _228 {
1214

13-
public static List<String> summaryRanges(int[] nums) {
14-
List<String> result = new ArrayList<>();
15-
for (int i = 0; i < nums.length; i++) {
16-
String start = String.valueOf(nums[i]);
17-
int tmpI = i;
18-
while ((i + 1) < nums.length && (nums[i] + 1) == nums[i + 1]) {
19-
i++;
20-
}
21-
if (tmpI == i) {
22-
result.add(start);
23-
} else {
24-
result.add(start + "->" + String.valueOf(nums[i]));
15+
public static class Solution1 {
16+
public List<String> summaryRanges(int[] nums) {
17+
List<String> result = new ArrayList<>();
18+
for (int i = 0; i < nums.length; i++) {
19+
String start = String.valueOf(nums[i]);
20+
int tmpI = i;
21+
while ((i + 1) < nums.length && (nums[i] + 1) == nums[i + 1]) {
22+
i++;
23+
}
24+
if (tmpI == i) {
25+
result.add(start);
26+
} else {
27+
result.add(start + "->" + String.valueOf(nums[i]));
28+
}
2529
}
26-
}
27-
return result;
28-
}
29-
30-
public static void main(String... args) {
31-
int[] nums = new int[]{0, 1, 2, 4, 5, 7};
32-
List<String> result = summaryRanges(nums);
33-
for (String s : result) {
34-
System.out.println(s);
30+
return result;
3531
}
3632
}
3733

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._228;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static junit.framework.Assert.assertEquals;
11+
12+
public class _228Test {
13+
private static _228.Solution1 solution1;
14+
private static List<String> expected;
15+
private static int[] nums;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _228.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
nums = new int[]{0, 1, 2, 4, 5, 7};
25+
expected = Arrays.asList("0->2", "4->5", "7");
26+
assertEquals(expected, solution1.summaryRanges(nums));
27+
}
28+
}

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