-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsearchInsert.js
50 lines (44 loc) · 1.02 KB
/
searchInsert.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
//二分法
var searchInsert = function(nums, target) {
var low = 0,
high = nums.length - 1,
mid;
while(low <= high){
mid = low + Math.floor((high - low) / 2);
if(target < nums[mid]){
if(mid === 0 || ((mid > 0) && nums[mid - 1] < target)){
return mid;
}
high = mid - 1;
}else if(target > nums[mid]){
low = mid + 1;
}else {
return mid;
}
}
return low;
};
console.log(searchInsert([1,2,3], 2.5));
// method two
var searchInsert2 = function(nums, target) {
var hash = {};
nums.forEach(function(item, index, array){
hash[item] = index;
});
if(hash[target] !== undefined){
return hash[target];
}
nums[-1] = -Number.MAX_VALUE;
nums[nums.length] = Number.MAX_VALUE;
for(var i = 0; i < nums.length; i++){
if(nums[i - 1] < target && nums[i] > target){
return i;
}
}
};
console.log(searchInsert([1,2,3], 0));