8000 add case · AldythNahak/leetcode@dc94432 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc94432

Browse files
committed
add case
1 parent e750c05 commit dc94432

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package find_first_and_last_position_of_element_in_sorted_array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public int[] searchRange(int[] nums, int target) {
8+
List<Integer> collectRange = new ArrayList<>();
9+
10+
for(int i = 0; i < nums.length; i++) {
11+
if(nums[i] == target) {
12+
collectRange.add(i);
13+
}
14+
}
15+
16+
if(collectRange.size() == 0) {
17+
return new int[]{-1,-1};
18+
} else if (collectRange.size() == 1) {
19+
collectRange.addLast(collectRange.getFirst());
20+
return collectRange.stream().mapToInt(i -> i).toArray();
21+
} else {
22+
return new int[]{collectRange.getFirst(), collectRange.getLast()};
23+
}
24+
}
25+
}
26+
27+
public class find_first_and_last_position_of_element_in_sorted_array {
28+
public static void main(String[] args) {
29+
Solution solution = new Solution();
30+
System.out.println(solution.searchRange(new int[] { 5, 7, 7, 8, 8, 10 }, 8));
31+
System.out.println(solution.searchRange(new int[] { 1 }, 1));
32+
System.out.println(solution.searchRange(new int[] { 3, 3, 3 }, 3));
33+
System.out.println(solution.searchRange(new int[] { 0, 0, 1, 2, 2 }, 2));
34+
}
35+
}

0 commit comments

Comments
 (0)
0