5 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
以 1 的位置為起點(diǎn),向上和向下循環(huán)(如有必要)數(shù)組:
const log = (arr, d) => console.log(`mimimal distance [${arr.join()}]: $nhkv1sx5v02o`);
const arr = [2, 0, 0, 0, 2, 2, 1, 0, 0, 2];
const arr2 = [1, 0, 0, 0, 2, 2, 2];
const arr3 = [2, 0, 1, 0, 2, 2, 2];
const arr4 = [2, 1, 0, 0, 2, 2, 2];
log(arr, clostes(arr));
log(arr2, clostes(arr2));
log(arr3, clostes(arr3));
log(arr4, clostes(arr4));
function clostes(arr) {
// determine position of 1
const indxOf1 = arr.indexOf(1);
// create array of distances
const distances = [0, 0];
// forward search
for (let i = indxOf1; i < arr.length; i += 1) {
if (arr[i] === 2) {
break;
}
distances[0] += arr[i] !== 2 ? 1 : 0;
}
// if 1 is @ position 0 backwards search
// is not necessary and minimum equals the
// already found maximum
if (indxOf1 < 1) {
distances[1] = distances[0];
return Math.min.apply(null, distances);
}
// backwards search
for (let i = indxOf1; i >= 0; i -= 1) {
if (arr[i] === 2) {
break;
}
distances[1] += arr[i] !== 2 ? 1 : 0;
}
return Math.min.apply(null, distances);
}

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
像這樣的東西可以完成這項(xiàng)工作。您可以使代碼更短,但我已嘗試說(shuō)明清楚。一旦我們找到1,從那個(gè)索引開(kāi)始并繼續(xù)檢查相鄰的索引。我們還進(jìn)行邊界檢查以確保我們不會(huì)溢出任何一端。
function closest(arr) {
const index = arr.findIndex(n => n === 1);
const len = arr.length;
let offset = 1;
while (true) {
const before = index - offset;
const after = index + offset;
const beforeBad = before < 0;
const afterBad = after >= len;
// It's necessary to check both, we could exceed the bounds on one side but not the other.
if (beforeBad && afterBad) {
break;
}
if ((!beforeBad && arr[before] === 2) || (!afterBad && arr[after] === 2)) {
return offset;
}
++offset;
}
return -1;
}

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
這個(gè)怎么樣:
Output = Input.map((cur,idx,arr)=>cur==2?Math.abs(idx-arr.indexOf(1)):Infinity).sort()[0]

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以在此處避免for循環(huán)以支持更實(shí)用的樣式。該函數(shù)minDist將m、n和 和作為參數(shù),并返回?cái)?shù)組中第一次出現(xiàn)的和任何出現(xiàn)的array之間的最小距離。mn
首先,map用于為每個(gè)元素創(chuàng)建一個(gè)數(shù)組,其中包含到目標(biāo)m元素的距離和當(dāng)前元素的值。然后filter用于僅保留表示n元素的對(duì)。Thensort用于表示最接近的元素的對(duì)位于數(shù)組的開(kāi)頭。最后,[0]排序后的數(shù)組的pair表示最近的元素[0],這個(gè)最近的pair的元素就是最小距離。
function minDist(m, n, array) {
let index = array.indexOf(m);
return array
.map((x, i) => [Math.abs(i - index), x])
.filter(p => p[1] === n)
.sort()[0][0];
}
console.log(minDist(1, 2, [1, 0, 0, 0, 2, 2, 2]));
console.log(minDist(1, 2, [2, 0, 0, 0, 2, 2, 1, 0, 0, 2]));

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以使用entries和reduce來(lái)解決這個(gè)問(wèn)題。
const arr = [2, 0, 0, 0, 2, 2, 1, 0, 0 ,2];
const goal = arr.indexOf(1);
const indices = [];
// Find all the indices of 2 in the array
for (let x of arr.entries()) {
if (x[1] === 2) indices.push(x[0]) ;
}
// Find the index that is closest to your goal
const nearestIndex = indices.reduce((prev, curr) => {
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
}); // 5
console.log(Math.abs(goal - nearestIndex)); // 1
添加回答
舉報(bào)