Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa5fdff

Browse files
committed
163 (1) trailing corner case
1 parent92b23a4 commitaa5fdff

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+
importjava.util.List;
18+
19+
/** see test {@link _163_MissingRanges.PracticeTest } */
20+
publicclassPractice {
21+
22+
publicList<String>findMissingRanges(int[]nums,intlower,intupper) {
23+
// TODO Auto-generated method stub
24+
returnnull;
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+
importjava.util.ArrayList;
21+
importjava.util.List;
22+
23+
/** see test {@link _163_MissingRanges.SolutionTest } */
24+
publicclassSolution {
25+
26+
publicList<String>findMissingRanges(int[]nums,intlower,intupper) {
27+
List<String>res =newArrayList<>();
28+
intexp =lower;
29+
inti =0;
30+
// don't forget the last possible missing range
31+
while (i <=nums.length &&exp <=upper) {
32+
intnum =i ==nums.length ?upper +1 :nums[i];
33+
if (num >upper) {
34+
num =upper +1;
35+
}
36+
if(num !=exp) {
37+
StringBuildersb =newStringBuilder("" +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+
returnres;
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+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
importorg.junit.After;
9+
importorg.junit.Before;
10+
importorg.junit.Rule;
11+
importorg.junit.Test;
12+
importorg.junit.rules.Timeout;
13+
14+
publicclassPracticeTest {
15+
16+
/** Test method for {@link _163_MissingRanges.Practice } */
17+
Practicesolution;
18+
19+
@Rule
20+
publicTimeoutglobalTimeout =newTimeout(200);
21+
22+
@Before
23+
publicvoidsetUp()throwsException {
24+
solution =newPractice();
25+
}
26+
27+
@After
28+
publicvoidtearDown()throwsException {
29+
solution =null;
30+
}
31+
32+
@Test
33+
publicvoidTest1() {
34+
int[]nums = {0,1,3,50,75 };
35+
intlower =0;
36+
intupper =99;
37+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
38+
List<String>expected =newArrayList<>();
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+
publicvoidTest2() {
48+
int[]nums = {0,1,2,3 };
49+
intlower =0;
50+
intupper =3;
51+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
52+
List<String>expected =newArrayList<>();
53+
assertEquals(expected,actual);
54+
}
55+
56+
@Test
57+
publicvoidTest3() {
58+
int[]nums = {1,3,5 };
59+
intlower =0;
60+
intupper =5;
61+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
62+
List<String>expected =newArrayList<>();
63+
expected.add("0");
64+
expected.add("2");
65+
expected.add("4");
66+
assertEquals(expected,actual);
67+
}
68+
69+
@Test
70+
publicvoidTest4() {
71+
int[]nums = {3,6 };
72+
intlower =0;
73+
intupper =5;
74+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
75+
List<String>expected =newArrayList<>();
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+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
importorg.junit.After;
9+
importorg.junit.Before;
10+
importorg.junit.Rule;
11+
importorg.junit.Test;
12+
importorg.junit.rules.Timeout;
13+
14+
publicclassSolutionTest {
15+
16+
/** Test method for {@link _163_MissingRanges.Solution } */
17+
Solutionsolution;
18+
19+
@Rule
20+
publicTimeoutglobalTimeout =newTimeout(200);
21+
22+
@Before
23+
publicvoidsetUp()throwsException {
24+
solution =newSolution();
25+
}
26+
27+
@After
28+
publicvoidtearDown()throwsException {
29+
solution =null;
30+
}
31+
32+
@Test
33+
publicvoidTest1() {
34+
int[]nums = {0,1,3,50,75 };
35+
intlower =0;
36+
intupper =99;
37+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
38+
List<String>expected =newArrayList<>();
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+
publicvoidTest2() {
48+
int[]nums = {0,1,2,3 };
49+
intlower =0;
50+
intupper =3;
51+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
52+
List<String>expected =newArrayList<>();
53+
assertEquals(expected,actual);
54+
}
55+
56+
@Test
57+
publicvoidTest3() {
58+
int[]nums = {1,3,5 };
59+
intlower =0;
60+
intupper =5;
61+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
62+
List<String>expected =newArrayList<>();
63+
expected.add("0");
64+
expected.add("2");
65+
expected.add("4");
66+
assertEquals(expected,actual);
67+
}
68+
69+
@Test
70+
publicvoidTest4() {
71+
int[]nums = {3,6 };
72+
intlower =0;
73+
intupper =5;
74+
List<String>actual =solution.findMissingRanges(nums,lower,upper);
75+
List<String>expected =newArrayList<>();
76+
expected.add("0->2");
77+
expected.add("4->5");
78+
assertEquals(expected,actual);
79+
}
80+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp