第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Javascript中的函數(shù),返回兩個(gè)給定數(shù)組索引的最近距離

Javascript中的函數(shù),返回兩個(gè)給定數(shù)組索引的最近距離

手掌心 2022-10-27 15:12:51
我想編寫(xiě)一個(gè)帶有 for 循環(huán)的函數(shù),該函數(shù)在數(shù)組中找到數(shù)字 1 的索引,并將差值返回到最接近數(shù)字 1 的數(shù)字 2 的索引(數(shù)字 1 只出現(xiàn)一次)。例如:Input: [1, 0, 0, 0, 2, 2, 2]Output: 4Input: [2, 0, 0, 0, 2, 2, 1, 0, 0 ,2]Output: 1我的嘗試function closest (array) {  let elem=array.findIndex(index=>index === 1)  let numberplus=0;  let numberminus=0;  for (let i=elem; i<array.length; i++){    if (array[elem+1] === 2)    {numberplus+=array[elem+1]-elem;}    break;        }  for (let i=elem; i>=0; i--) {    if (array[elem-1] ===2)    {numberminus+=array[elem-1]-elem;}    break;    }   if (numberplus < numberminus) {      return numberplus   } else {   return numberminus}   }調(diào)用時(shí),該函數(shù)僅返回“0”。謝謝閱讀!
查看完整描述

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);

}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
LEATH

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;

}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
智慧大石

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]



查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
浮云間

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]));


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以使用entriesreduce來(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


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 5 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)