如果数字已插入排序数组,则获取该数字的索引

Get index of number if it was inserted into a sorted array

本文关键字:数字 获取 索引 插入排序 数组 如果      更新时间:2023-09-26

function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
    return a - b;
  });
  
  for (var i = 0; i < arr.length; i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
      return i; // return index pos of num bigger than value
    }
    else if (arr[i] === undefined) { // if not found 
      arr.push(num); // push to array 
      return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}
console.log(getIndexToIns([2, 5, 10], 15)); // Should return 3

这样做的任务是对数组进行排序,并返回arg2的索引值(如果它在数组中)。

示例:getIndexToIns([10, 20, 30, 40, 50], 35)应返回3

我遇到的问题是,如果在数组中找不到arg2,则将其推入数组并返回其索引值。我似乎做不到。

另一种方法:

function getIndex(arr, num) {
  return arr.concat(num).sort(function(a, b) {
    return a - b;
  }).indexOf(num);
}

当然有几种方法可以做到这一点,但代码中的修复方法如下:

工作示例

function getIndexToIns(arr, num) {
  arr.sort(function(a,b) {
    return a-b;
  });
  for (var i=0;i<arr.length;i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
        return i; // return index pos of num bigger than value
    }
    if (i === arr.length - 1) { // if not found 
        arr.push(num); // push to array 
        return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}

在您的代码中,您检查了if (arr[i] === undefined),但这永远不会发生,因此请检查您是否在数组的末尾,如果是,则意味着您尚未找到您的编号,然后您可以推送它并获取索引。

为什么不在数组上使用.push.indexOf方法?

function arrSort(a, b) {
    return a - b;
}
function getIndexToIns(arr, num) {
    // you sort the array
    arr.sort(arrSort);
    // if it doesn't contain the num
    if(arr.indexOf(num) == -1) {
        // add the num to the array
        arr.push(num);
        // sort the array again
        arr.sort(arrSort);
        // return the index of the num
        return arr.indexOf(num);
    }
    // if the num is in the array, return its position
    return arr.indexOf(num);
}

由于您的数组似乎已经排序,您应该只使用二分法搜索来查找索引,然后使用splice插入它。

function getIndexToIns(arr, num) {
  var index = (function search(from, to) {
    if(from == to) return to;
    var m = Math.floor((from+to)/2);
    if(arr[m] > num) return search(from, m);
    if(arr[m] < num) return search(m+1, to);
    return m;
  })(0, arr.length);
  arr.splice(index, 0, num);
  return index;
}

或者,由于它无论如何都是线性的,手动向后循环:

function getIndexToIns(arr, num) {
  for(var i=arr.length; i>0 && arr[i-1]>num; --i) arr[i] = arr[i-1];
  arr[i] = num;
  return i;
}

您可以将num推入数组,然后用mapsort对其进行排序。

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.map(function(a,b) {
    a-b;
     arr.indexOf(num);
  });
  console.log(arr+''n');
  console.log(num +' is at '+arr.indexOf(num)+''n');
}
getIndexToIns([2, 5, 10], 15);

function getIndexToIns(arr, num) {
    function compare(a,b){
        return  a-b;
    }
    arr.push(num);
    arr.sort(compare);
    console.log(num);
    num = arr.indexOf(num);
    return num;
}

getIndexToIns([40,60],50);

我对使用了,并嵌套了两个if条件,即变量jk中的索引计数。

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var j = 0;
  var k = 0;
  
  arr.sort(function(a, b){return a - b;});
  for (var i= 0; i < arr.length; i++){
    if ( arr[i] < num ) {
      k++;      
      if (arr[i] > num) {
        j++;
      }
    }
  }
  return j+k;
}
console.log(getIndexToIns([3, 10, 5], 3));
console.log(getIndexToIns([10, 20, 30, 40, 50], 35));
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
console.log(getIndexToIns([40, 60], 50));
console.log(getIndexToIns([5, 3, 20, 3], 5));
console.log(getIndexToIns([2, 20, 10], 19));
console.log(getIndexToIns([2, 5, 10], 15));