# 面试刷题偶有记录

​
class Solution {
public int[] searchRange(int[] nums, int target) {
int left = searchRangeLeft(nums, target);
int right = searchRangeRight(nums, target);
int[] res = {left, right};
return res;
}

private int searchRangeRight(int[] a, int v){
int l = 0;
int r = a.length - 1;
int res = -1;
while(l <= r){
int mid = l + ((r-l)>>1);
if(a[mid] < v){
l = mid + 1;
}else if(a[mid] > v){
r = mid - 1;
}else{
res = mid;
l = mid + 1;
}
}
return res;
}
private int searchRangeLeft(int[] a, int v){
int l = 0;
int r = a.length - 1;
int res = -1;
while(l <= r){
int mid = l + ((r-l)>>1);
if(a[mid] > v){
r = mid - 1;
}else if(a[mid] < v){
l = mid + 1;
}else{
res = mid;
r = mid - 1;
}
}
return res;
}
}

​

class Solution {
public int[] searchRange(int[] nums, int target) {
int left = searchRangeLeft(nums, target);
int right = searchRangeRight(nums, target);
int[] res = {left, right};
return res;
}

public int searchRangeLeft(int[] nums, int target){
int low = 0;
int high = nums.length -1;
int mid;
while(low<high){
mid = low+(high-low)/2;
if(nums[mid] >= target){
high = mid;
}else{
low = mid+1;
}
}
return nums[low]==target?low:-1;
}

public int searchRangeRight(int[] nums, int target){
int low = 0;
int high = nums.length -1;
while(low<high){
int mid = (low+high)/2 + 1;
if(nums[mid] <= target){
low = mid;
}else{
high = mid-1;
}
}
return nums[low]==target?low:-1;
}
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

绝命毒师J

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24
07-15 3385

02-04 1万+
10-27 399
05-13 64
04-24 362
08-31 1086
10-28 261
04-18 869