如果数字已插入排序数组,则获取该数字的索引
Get index of number if it was inserted into a sorted array
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
推入数组,然后用map
或sort
对其进行排序。
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条件,即变量j和k中的索引计数。
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));
相关文章:
- 在循环中获取从1到x的数字
- 如果数字已插入排序数组,则获取该数字的索引
- javascript从id获取数字
- 如何获取一个以零开头的数字的位数
- 忽略任何非小写字符(如数字、大写字母和符号)并获取k的值
- 使用Regex获取括号内的数字
- 用于获取两个方括号之间的数字的正则表达式
- 如何获取数组中数字的最大出现次数
- 获取对象的所有可能组合,其中值的总和与数字匹配
- 从数字字符串中获取整数
- jQuery UI Slider ui.value 获取最后一个数字
- 在 JavaScript 中使用最后两个数字获取年份
- 无法可视化百分比数字(获取 NaN)
- 通过正则表达式获取数字
- 在数字索引循环中获取数组项的键
- 获取滑块以显示月份而不是数字
- 正在获取NaN而不是数字/值
- 如何在javascript中获取隐藏字段的数字和
- 如何根据月份中的数字获取日期名称
- 使用输入类型数字获取光标位置