# Leetcode

## Find First and Last Position of Element in Sorted Array

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

class Solution {
public int[] searchRange(int[] nums, int target) {
int[] output = {-1, -1};
if (nums.length == 0) return output;
int left = 0, right = nums.length - 1;
while (left <= right){
int pivot = (left + right) / 2;
if(nums[pivot] == target){
output = extension(nums, pivot, target);
return output;
}
if (target < nums[pivot])
right = pivot - 1;
else
left = pivot + 1;
}
return output;
}
private int[] extension(int[] nums, int pivot, int target){
int[] output = new int[2];
int h = pivot;
int l = pivot;
while(h <= nums.length - 1 &&nums[h] == target){
h ++;
}
while(l >= 0 && nums[l] == target){
l--;
}
output[0] = l + 1;
output[1] = h - 1;
return output;
}
}


searchRange用了binary search。就如同正常的binary search一般，只不过找到条件在返回前，先实现extension 方法。

Tag：binary search。

class Solution {
// returns leftmost (or rightmost) index at which target should be
// inserted in sorted array nums via binary search.
private int extremeInsertionIndex(int[] nums, int target, boolean left) {
int lo = 0;
int hi = nums.length;

while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] > target || (left && target == nums[mid])) {
hi = mid;
}
else {
lo = mid+1;
}
}

return lo;
}

public int[] searchRange(int[] nums, int target) {
int[] targetRange = {-1, -1};

int leftIdx = extremeInsertionIndex(nums, target, true);

// assert that leftIdx is within the array bounds and that target
// is actually in nums.
if (leftIdx == nums.length || nums[leftIdx] != target) {
return targetRange;
}

targetRange[0] = leftIdx;
targetRange[1] = extremeInsertionIndex(nums, target, false)-1;

return targetRange;
}
}


if (nums[mid] > target || (left && target == nums[mid]))


## Search insert position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right){
int pivot = (left + right) / 2;
if(nums[pivot] == target)
return pivot;
if (target > nums[pivot])
left = pivot + 1;
else
right = pivot - 1;
}
return left;
}
}


11-07 1118

05-20 17

08-14 15

05-20 32

07-04 417

05-20 172

03-15 571

04-28 113

11-12 2万+

10-19 286

11-19 7702

04-02 60

02-19 113

11-28 631

03-15 2471

07-18 53

04-19 23

08-29 1373

03-12 442

04-20 1654

04-14 59万+

03-13 15万+

03-04 13万+

03-08 2万+

04-25 6万+

03-10 13万+

03-10 18万+

03-12 11万+

03-13 11万+

03-18 9739

03-19 8万+

03-19 3万+

03-22 4万+

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 6059

05-28 1万+

04-02 1万+

04-02 4999

04-02 4万+

04-06 9655

04-06 7万+

04-09 8万+

04-09 2万+

05-17 8404

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

05-01 6377

05-03 1万+

05-16 5万+

05-06 1万+

05-07 2181

05-07 1万+

05-08 4万+

05-10 3232

05-14 6401

05-16 3540

05-16 1万+

05-17 5050

05-30 229

05-20 949

05-18 2202

05-18 8346

05-27 1335

05-18 3528

05-28 1163

05-19 1万+

05-19 1906

05-21 7276

#### 石锤！今年Python要过苦日子了？ 程序员：我疯了！

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客