parsing

Method 1

Order log n time complexity algorithm, determines the search algorithm is binary search. The rest is judgment.

/ * * *@param {number[]} nums
 * @param {number} target
 * @return {number}* /
var searchInsert = function(nums, target) {
    //logn is binary search
    let low = 0,high = nums.length-1
    // Insert subscript value when not found
    let noFindIndex = 0
     while (low <= high) {
          let mid = Math.floor((low + high) / 2);
          // Save the middle value of the calculation each time
          noFindIndex = mid
          if(target === nums[mid]){
              // Find the subscript
              return mid
          }else if(target < nums[mid]){
               high = mid - 1
          }else if(target > nums[mid]){
               low = mid + 1}}// If no target value is found, noFindIndex is the last intermediate value
     if(target < nums[noFindIndex]){
       	 NoFindIndex is returned if the target value < the value of the last intermediate subscript
         return noFindIndex
     }else {
         NoFindIndex +1 is returned if no value is found and the target value is > the value of the last intermediate subscript
         return noFindIndex + 1}};Copy the code