10000 refactor 352 · arnav1996/Leetcode-1@92f32d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92f32d6

Browse files
refactor 352
1 parent b772914 commit 92f32d6

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,46 @@
2424
*/
2525
public class _352 {
2626

27-
public static class SummaryRanges {
27+
public static class Solution1 {
28+
public static class SummaryRanges {
2829

29-
/**
30-
* Use treemap, key is the start of the interval.
31-
*/
32-
TreeMap<Integer, Interval> treeMap;
30+
/**
31+
* Use treemap, key is the start of the interval.
32+
*/
33+
TreeMap<Integer, Interval> treeMap;
3334

34-
/**
35-
* Initialize your data structure here.
36-
*/
37-
public SummaryRanges() {
38-
treeMap = new TreeMap<>();
39-
}
40-
41-
public void addNum(int val) {
42-
if (treeMap.containsKey(val)) {
43-
return;
35+
/**
36+
* Initialize your data structure here.
37+
*/
38+
public SummaryRanges() {
39+
treeMap = new TreeMap<>();
4440
}
45-
Integer lower = treeMap.lowerKey(val);
46-
Integer higher = treeMap.higherKey(val);
47-
if (lower != null && higher != null && treeMap.get(lower).end + 1 == val && higher == val + 1) {
48-
treeMap.get(lower).end = treeMap.get(higher).end;
49-
treeMap.remove(higher);
50-
} else if (lower != null && treeMap.get(lower).end + 1 >= val) {
51-
treeMap.get(lower).end = Math.max(treeMap.get(lower).end, val);
52-
} else if (higher != null && higher == val + 1) {
53-
treeMap.put(val, new Interval(val, treeMap.get(higher).end));
54-
treeMap.remove(higher);
55-
} else {
56-
treeMap.put(val, new Interval(val, val));
41+
42+
public void addNum(int val) {
43+
if (treeMap.containsKey(val)) {
44+
return;
45+
}
46+
Integer lower = treeMap.lowerKey(val);
47+
Integer higher = treeMap.higherKey(val);
48+
if (lower != null
49+
&& higher != null
50+
&& treeMap.get(lower).end + 1 == val
51+
&& higher == val + 1) {
52+
treeMap.get(lower).end = treeMap.get(higher).end;
53+
treeMap.remove(higher);
54+
} else if (lower != null && treeMap.get(lower).end + 1 >= val) {
55+
treeMap.get(lower).end = Math.max(treeMap.get(lower).end, val);
56+
} else if (higher != null && higher == val + 1) {
57+
treeMap.put(val, new Interval(val, treeMap.get(higher).end));
58+
treeMap.remove(higher);
59+
} else {
60+
treeMap.put(val, new Interval(val, val));
61+
}
5762
}
58-
}
5963

60-
public List<Interval> getIntervals() {
61-
return new ArrayList<>(treeMap.values());
64+
public List<Interval> getIntervals() {
65+
return new ArrayList<>(treeMap.values());
66+
}
6267
}
6368
}
6469
}

src/test/java/com/fishercoder/_352Test.java

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,35 @@
88

99
import java.util.List;
1010

11-
/**
12-
* Created by stevesun on 6/1/17.
13-
*/
1411
public class _352Test {
15-
private static _352.SummaryRanges test;
16-
private static List<Interval> actual;
17-
18-
@BeforeClass
19-
public static void setup() {
20-
test = new _352.SummaryRanges();
21-
}
22-
23-
@Test
24-
public void test1() {
25-
test.addNum(1);
26-
actual = test.getIntervals();
27-
CommonUtils.printIntervals(actual);
28-
29-
test.addNum(3);
30-
actual = test.getIntervals();
31-
CommonUtils.printIntervals(actual);
32-
33-
test.addNum(7);
34-
actual = test.getIntervals();
35-
CommonUtils.printIntervals(actual);
36-
37-
test.addNum(2);
38-
actual = test.getIntervals();
39-
CommonUtils.printIntervals(actual);
40-
41-
test.addNum(6);
42-
actual = test.getIntervals();
43-
CommonUtils.printIntervals(actual);
44-
}
12+
private static _352.Solution1.SummaryRanges test;
13+
private static List<Interval> actual;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
test = new _352.Solution1.SummaryRanges();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
test.addNum(1);
23+
actual = test.getIntervals();
24+
CommonUtils.printIntervals(actual);
25+
26+
test.addNum(3);
27+
actual = test.getIntervals();
28+
CommonUtils.printIntervals(actual);
29+
30+
test.addNum(7);
31+
actual = test.getIntervals();
32+
CommonUtils.printIntervals(actual);
33+
34+
test.addNum(2);
35+
actual = test.getIntervals();
36+
CommonUtils.printIntervals(actual);
37+
38+
test.addNum(6);
39+
actual = test.getIntervals();
40+
CommonUtils.printIntervals(actual);
41+
}
4542
}

0 commit comments

Comments
 (0)
0