标签:必须 round ons 目标 lock splay display cti 首部
var twoSum = function (numbers, target) { let l = 0, r = numbers.length - 1; while (l < r) { let sum = numbers[l] + numbers[r]; if (sum === target) { // 若双指针的和等于targe 那么对应下标就是要找的 return [l + 1, r + 1]; } else if (sum > target) r--; // 如果双指针的和>目标值 那么右侧减一 else l++; // 双指针的和<目标值 左侧加一 } };
var twoSum=function(numbers,target){ for(let i=0;i<numbers.length;i++){ let other=target-numbers[i] let res=binarySearch(numbers,other) if(res!=-1&&i!=res){ return i<res?[i+1,res+1]:[res+1,i+1] } } return [] } const binarySearch = function(numbers, target) { let high=numbers.length let low=0 while(low<=high){ let mid=parseInt((low+high)/2) if(numbers[mid]===target){ return mid }else if(numbers[mid]<target){ low=mid+1 }else{ high=mid-1 } } return -1 };
标签:必须 round ons 目标 lock splay display cti 首部
原文地址:https://www.cnblogs.com/lanpang9661/p/12427442.html