-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindIndexedPoint.js
32 lines (30 loc) · 1 KB
/
findIndexedPoint.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
/*
* 判断数组中是否含有坐标点的值跟坐标index相同的元素,即A[i]=i。复杂度为O(logn)
* 原题目:You are given a sorted (from smallest to largest) array
A of n distinct integers which can be positive, negative, or zero. You
want to decide whether or not there is an index i such that A[i] = i.
Design the fastest algorithm you can for solving this problem
* */
function findeIndexedPoint (arr, start) {
var start = start || 0;
var l = arr.length;
var mid = Math.floor(l/2);
if (l === 1) {
if (arr[0] === start) {
console.log(arr[0] + " : " + start);
return true;
} else {
return false;
}
}
var leftHas = findeIndexedPoint(arr.slice(0, mid), start);
var rightHas = findeIndexedPoint(arr.slice(mid, l), mid + start);
if (leftHas || rightHas) {
return true;
} else {
return false;
}
}
/*
* 算法说明:使用二分归并查找,并传入padding值
* */