function search( nums , target ){// write code here
let left =0,right = nums.length -1while(left <= right){
let mid = Math.floor((left+right)/2)
let num = nums[mid]if(target == num){return mid
}elseif(num > target){
right = mid -1}else{
left = mid +1}}return-1}
18 二维递增数组查找(行列均递增)
function Find(target, array){// write code here
let n = array[0].length
for(let i=0;i<array.length;i++){
let temp = array[i]for(let j=0;j<n;j++){if(temp[j]== target){return true
}elseif(temp[j]>target){if(j ==0){return false
}else{
n=j
}}}}}
19 寻找数组峰值
function findPeakElement( nums ){// write code here
let left =0,right = nums.length-1while(left<right){
let mid = Math.floor((left+right)/2)if(nums[mid]< nums[mid+1]){
left = mid+1}else{
right = mid
}}return right
}
20 数组中的逆序对【好难 继续写归并】
function InversePairs(data){// write code here
let count =0mergesort(data)
function mergesort(arr){if(arr.length <2)return arr
let mid = Math.floor(arr.length/2)
let left = arr.slice(0,mid)
let right = arr.slice(mid)
function mergetwo(left,right){
let result =[]while(left.length && right.length){if(left[0]>right[0]){
count = count + left.length
}
result.push(left[0]<=right[0]?left.shift():right.shift())}return result.concat(left,right)}returnmergetwo(mergesort(left),mergesort(right))}return count %1000000007}
21 旋转数组的最小数字(非递减数组旋转 比如[3,4,5,2,2])
function minNumberInRotateArray(rotateArray){// write code here
let arr = rotateArray
if(arr.length <2)return arr
if(arr.length ==2)return Math.min(arr[0],arr[1])
let mid = Math.floor(arr.length/2)
let midvalue = arr[mid]
let left = arr.slice(0,mid+1)
let right = arr.slice(mid+1)
let leftmost = arr[0],rightmost = arr[arr.length -1]if(leftmost>midvalue)returnminNumberInRotateArray(left)elseif(midvalue>rightmost)returnminNumberInRotateArray(right)elsereturn Math.min(minNumberInRotateArray(left),minNumberInRotateArray(right))}
22 比较版本号 (比如1.0.2 > 1.0.1)
function compare( version1 , version2 ){// write code here
let arr1 = version1.split('.')
let arr2 = version2.split('.')
let i =0while(i<arr1.length || i<arr2.length){
let x=0,y=0if(i<arr1.length) x=parseInt(arr1[i])if(i<arr2.length) y=parseInt(arr2[i])if(x>y)return1if(x<y)return-1
i++}return0}