leetcode-34. 在排序数组中查找元素的第一个和最后一个位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public int[] searchRange(int[] nums, int target) {
int n = nums.length;
int i = -1;
int j = n - 1;
int a = -1;
int b = -1;
while(i < n-1){
i++;
if(nums[i] == target){
a = i;
break;
}
}

while(j >= 0){
if(nums[j] == target){
b = j;
break;
}
j --;
}
return new int[]{a, b};
}
}