2017-04-02 71 views
1

我刪除了這一點,因爲我不相信這是一個很好的問題重複的數字

+0

所以請告訴我們你開始與 – STF

+0

你能解釋一下你想要什麼?它不清楚輸入和輸出的描述。 –

+0

_「我的不好。Outprints:1,1,4,4,4,5,5,5,5」_您能編輯原始問題以包含預期結果。 – guest271314

回答

0

,如果你真的想在字符串,我將採取兩個字符串和他們每個人的轉換成數組怎麼寫,例如:[ 2,3,4]和[1,4,5]然後concatonate他們。然後使用簡單的數據結構算法按順序排列它們。因此,例如,遍歷數組中的每個項目,並將其移動到0位置(如果它是最小的數字)。然後轉到下一個位置,並嘗試移動陣列中除第一個位置以外的最小編號到第二個位置等。

0

我有一個想法,我認爲應該有一個重複函數。例如,它可以重複'a'3次。像a => [a, a, a]

function repeat(a, times) { 
    if (times === 0) return []; 
    return [a].concat(repeat(a, times - 1)); 
} 

然後我用它來完成這個問題。

function repeatAndConcat(times, arr) { 
    return arr.map(function(item, index) { 
    return repeat(item, times[index]); 
    }).reduce(function(a, b) { 
    return a.concat(b); 
    }); 
} 

repeatConcat([2,3,4],[1,4,5])與回報[1,1,4,4,4,5,5,5,5]。

最後減少平整一個嵌套數組。

+0

我喜歡。它可以工作!謝謝! – WebNeoRaven

+0

@WebNeoRaven很高興我能幫助你 – g1eny0ung

0

可以使用Array.prototype.entries()for..of環,散佈元件,Array.prototype.fill()創建陣列,其具有通過在第一個數組元素的值設置.length,在第二陣列填充有值在相同的索引

let [len, arr, res] = [[2,3,4], [1,4,5], []]; 
 

 
for (let [key, prop] of arr.entries()) res.push(...Array(len[key]).fill(prop)); 
 

 
console.log(res);