题解 | #查找数组元素位置#
查找数组元素位置
http://www.nowcoder.com/practice/e7835a8113dd48afb15f77ef8d1dcb1d
二分查找方法
其时间复杂度: O(log2n)
function indexOf(arr, item) { let left = 0, mid, right = arr.length - 1; while(left <= right) { mid = parseInt((left + right) / 2) if(item == arr[mid]) { return mid } else if(item > arr[mid]) { left = mid + 1 } else { right = mid - 1 } } return -1 }