Skip to content

Commit aa5fdff

Browse files
committed
163 (1) trailing corner case
1 parent 92b23a4 commit aa5fdff

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

src/_163_MissingRanges/Practice.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
***************************************************************************
3+
* Description:
4+
*
5+
* Given a sorted integer array where the range of elements are [lower, upper]
6+
* inclusive, return its missing ranges.
7+
*
8+
* For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99,
9+
* return ["2", "4->49", "51->74", "76->99"].
10+
*
11+
***************************************************************************
12+
* @tag : Array
13+
* {@link https://leetcode.com/problems/missing-ranges/ }
14+
*/
15+
package _163_MissingRanges;
16+
17+
import java.util.List;
18+
19+
/** see test {@link _163_MissingRanges.PracticeTest } */
20+
public class Practice {
21+
22+
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
23+
// TODO Auto-generated method stub
24+
return null;
25+
}
26+
27+
}

src/_163_MissingRanges/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Time : O(N) ; Space: O()
3+
* @tag : Array
4+
* @by : Steven Cooks
5+
* @date: Sep 30, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given a sorted integer array where the range of elements are [lower, upper]
10+
* inclusive, return its missing ranges.
11+
*
12+
* For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99,
13+
* return ["2", "4->49", "51->74", "76->99"].
14+
*
15+
***************************************************************************
16+
* {@link https://leetcode.com/problems/missing-ranges/ }
17+
*/
18+
package _163_MissingRanges;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
/** see test {@link _163_MissingRanges.SolutionTest } */
24+
public class Solution {
25+
26+
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
27+
List<String> res = new ArrayList<>();
28+
int exp = lower;
29+
int i = 0;
30+
// don't forget the last possible missing range
31+
while (i <= nums.length && exp <= upper) {
32+
int num = i == nums.length ? upper + 1 : nums[i];
33+
if (num > upper) {
34+
num = upper + 1;
35+
}
36+
if(num != exp) {
37+
StringBuilder sb = new StringBuilder("" + exp);
38+
if (num != exp + 1) {
39+
sb.append("->").append(num - 1);
40+
}
41+
res.add(sb.toString());
42+
}
43+
exp = num + 1;
44+
i++;
45+
}
46+
return res;
47+
}
48+
49+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package _163_MissingRanges;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.Timeout;
13+
14+
public class PracticeTest {
15+
16+
/** Test method for {@link _163_MissingRanges.Practice } */
17+
Practice solution;
18+
19+
@Rule
20+
public Timeout globalTimeout = new Timeout(200);
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
solution = new Practice();
25+
}
26+
27+
@After
28+
public void tearDown() throws Exception {
29+
solution = null;
30+
}
31+
32+
@Test
33+
public void Test1() {
34+
int[] nums = { 0, 1, 3, 50, 75 };
35+
int lower = 0;
36+
int upper = 99;
37+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
38+
List<String> expected = new ArrayList<>();
39+
expected.add("2");
40+
expected.add("4->49");
41+
expected.add("51->74");
42+
expected.add("76->99");
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Test
47+
public void Test2() {
48+
int[] nums = { 0, 1, 2, 3 };
49+
int lower = 0;
50+
int upper = 3;
51+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
52+
List<String> expected = new ArrayList<>();
53+
assertEquals(expected, actual);
54+
}
55+
56+
@Test
57+
public void Test3() {
58+
int[] nums = { 1, 3, 5 };
59+
int lower = 0;
60+
int upper = 5;
61+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
62+
List<String> expected = new ArrayList<>();
63+
expected.add("0");
64+
expected.add("2");
65+
expected.add("4");
66+
assertEquals(expected, actual);
67+
}
68+
69+
@Test
70+
public void Test4() {
71+
int[] nums = { 3, 6 };
72+
int lower = 0;
73+
int upper = 5;
74+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
75+
List<String> expected = new ArrayList<>();
76+
expected.add("0->2");
77+
expected.add("4->5");
78+
assertEquals(expected, actual);
79+
}
80+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package _163_MissingRanges;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.Timeout;
13+
14+
public class SolutionTest {
15+
16+
/** Test method for {@link _163_MissingRanges.Solution } */
17+
Solution solution;
18+
19+
@Rule
20+
public Timeout globalTimeout = new Timeout(200);
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
solution = new Solution();
25+
}
26+
27+
@After
28+
public void tearDown() throws Exception {
29+
solution = null;
30+
}
31+
32+
@Test
33+
public void Test1() {
34+
int[] nums = { 0, 1, 3, 50, 75 };
35+
int lower = 0;
36+
int upper = 99;
37+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
38+
List<String> expected = new ArrayList<>();
39+
expected.add("2");
40+
expected.add("4->49");
41+
expected.add("51->74");
42+
expected.add("76->99");
43+
assertEquals(expected, actual);
44+
}
45+
46+
@Test
47+
public void Test2() {
48+
int[] nums = { 0, 1, 2, 3 };
49+
int lower = 0;
50+
int upper = 3;
51+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
52+
List<String> expected = new ArrayList<>();
53+
assertEquals(expected, actual);
54+
}
55+
56+
@Test
57+
public void Test3() {
58+
int[] nums = { 1, 3, 5 };
59+
int lower = 0;
60+
int upper = 5;
61+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
62+
List<String> expected = new ArrayList<>();
63+
expected.add("0");
64+
expected.add("2");
65+
expected.add("4");
66+
assertEquals(expected, actual);
67+
}
68+
69+
@Test
70+
public void Test4() {
71+
int[] nums = { 3, 6 };
72+
int lower = 0;
73+
int upper = 5;
74+
List<String> actual = solution.findMissingRanges(nums, lower, upper);
75+
List<String> expected = new ArrayList<>();
76+
expected.add("0->2");
77+
expected.add("4->5");
78+
assertEquals(expected, actual);
79+
}
80+
}

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