问题描述
假设我有两个数组对:
let a1 = [2000, 4000, 6000, 8000, 10000, 12000],
b1 = [2000, 4000, 6000, 8000, 10000, 12000, 14000];
和
let a2 = [10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000],
b2 = [10000, 20000, 30000, 40000, 50000, 60000];
或任意任意一对整数.
任务是通过这种方式将对中的最小数组扩展为最大大小,同时使最小域和值保持不变:
The task is to expand the smallest array in the pair to largest size in this way, keeping smallest domain the same as well as values:
b1 = [2000, 4000, 6000, 8000, 10000, 12000, 14000];
expanded_a1 = [2000, 4000, 6000, 8000, 10000, 12000, 12000];
a2 = [10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000]
expanded_b2 = [10000, 20000, 20000, 30000, 30000, 40000, 40000, 50000, 50000, 60000];
invalid_expanded_b2 = [10000, 15000, 20000, 30000, 30000, 40000, 40000, 50000, 50000, 60000];
//second value is invalid
再次,以第三对为例:
let a3 = [10, 15, 20, 25, 30, 35];
let b3 = [10, 20, 30];
因此,扩展的b3应该只有10,20,30个值,并且其域必须为[10,30]
So, expanded b3 should have only 10,20,30 values and its domain have to be [10,30]
[10, 15, 20, 25, 30, 35]
↓ ↓ ↓ ↓ ↓ ↓
expanded_b3 = [10, 20, 20, 30, 30, 30]
很确定,首先,我需要克隆最大的数组并遍历它的每个元素,并将其与最小数组中的整数进行比较,如果值不匹配,则将其替换为最左边的整数(或者最小数组中没有这样的(右)整数.
Pretty sure, that, first of all, I need to clone the largest array and go through its every element and compare it with integers from the smallest array and if values don't match replace it with the closest left (or if there is no such, the right) integer from the smallest array.
推荐答案
乍一看似乎很棘手,但实际上并不难.您只需要一起浏览两个数组.可以将其想象为获得两个指针,然后将它们向前移动:
This seems tricky to do at first but it's actually not that hard. You just have to go through both arrays together. Think of it as getting two pointers and then moving them along:
a2 = [10000, 15000, 20000]
^^^^^
|
pointer 1 ----
b2 = [10000, 20000]
^^^^^
|
pointer 2 ----
- 如果分配给每个指针的当前值相等,则您有一个匹配项-只需返回该值并移动两个指针即可
a2 = [10000, 15000, 20000]
^^^^^
|
pointer 1 -----------
b2 = [10000, 20000]
^^^^^
|
pointer 2 -----------
- 如果当前值不匹配,则仅前进属于较长数组的指针,而从较短数组返回该值
a2 = [10000, 15000, 20000]
^^^^^
|
pointer 1 ------------------
b2 = [10000, 20000]
^^^^^
|
pointer 2 -----------
重复直到完成更长的阵列.
Repeat until you finish the longer array.
您可以在 O(n)
时间内完成此操作.
You can do this in O(n)
time.
这是使用 Generator 和ES6数组迭代器都可以数组.它比使用指针稍微方便一些,因为您可以使用 Array.from
Here is an implementation using a Generator and the ES6 array Iterators to walk both arrays. It is slightly more convenient than using pointers, since you can directly generate an array using Array.from
let a1 = [2000, 4000, 6000, 8000, 10000, 12000],
b1 = [2000, 4000, 6000, 8000, 10000, 12000, 14000];
let a2 = [10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000],
b2 = [10000, 20000, 30000, 40000, 50000, 60000];
function equaliseLength(arr1, arr2) {
//equal length, nothing to do, just return
if (arr1.length === arr2.length) return;
//setup what to work with
let longerArray;
let arrayToExpand;
if (arr1.length > arr2.length) {
longerArray = arr1;
arrayToExpand = arr2;
} else {
longerArray = arr2;
arrayToExpand = arr1;
}
const generator = generateMissingSlots(arrayToExpand, longerArray);
//convert to array
const result = Array.from(generator);
return result;
}
function* generateMissingSlots(shorter, longer) {
//start two iterators for each array
const shortIterator = shorter[Symbol.iterator]();
const longIterator = longer[Symbol.iterator]();
//get the initial values
let shortCurrent = shortIterator.next();
let longCurrent = longIterator.next();
let lastShortValue = shortCurrent.value;
//go through both until the longer is exhausted
while(!longCurrent.done) {
//produce the current value of the shorter array
yield lastShortValue;
//only advance the short iterator if the two have "caught up"
if (shortCurrent.value === longCurrent.value){
shortCurrent = shortIterator.next();
//update the value unless the iterator is exhausted
//in case of `short = [a, b]` and `long = [a, b, c]` which will produce `undefined` for the last values
if(!shortCurrent.done) {
lastShortValue = shortCurrent.value
}
}
//always advance the long iterator
longCurrent = longIterator.next();
}
}
expanded_a1 = equaliseLength(a1, b1);
console.log(expanded_a1);
expanded_b2 = equaliseLength(a2, b2);
console.log(expanded_b2);
这篇关于如何匹配两个整数数组,因此最小的数组会从较大的数组扩展到最近的邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!