2016-08-16 77 views
-1

我試圖解決這個難題重複的條目,下方則是指令的Javascript計數字符串

現在給你一個字符串數組strarr和整數k。您的任務是返回數組中第一個由連續k個字符串組成的最長字符串。

實施例:

longest_consec([ 「區」, 「亞比」, 「THETA」, 「形式」, 「libe」, 「ZAS」, 「THETA」> 「亞比」],2) - - >「abigailtheta」 n是字符串數組的長度,如果(n = 0)或(k> n)或(k < = 0)返回「」;

下面是我到目前爲止工作過的代碼。我有解釋評論他們。

function longestConsec(strarr, k) { 

if((strarr.length == 0) || (k > strarr.length) || (k <= 0)){ 
    return ""; // solves return of empty string 
    } 

    var empty = ''; 
    var str = strarr.split(' '); // then splits into an array which can be cycled through. 

for (var i = 0; i < strarr.length; i++){ // cycle through length; 
    for(var j = 0; j < strarr[i]; j++){ // cycle through ontop of that 
    if (strarr[i] === strarr[j]){ // compare if any of cycle 1 = cycle 2 
     empty.concat(strarr[i]); // if it does, concat the word into a string 
     } 
    } 
    } 
} 
+0

???多次閱讀你的問答,例子答案和答案是沒有意義的。 ?字符串的順序不重要嗎?如果是這樣,這意味着每個字符串的內容相同嗎? – Blindman67

+0

字符串'.concat()'方法返回* new *字符串,它不會修改現有的'empty'字符串。 – nnnnnn

+0

原來的問題很糟糕。我可以閱讀每一個字,但無法理解這句話。 – Leo

回答

2
function longest_consec(strarr, k) { 
    var longest = ''; 
    for (var i = 0, testString; i <= strarr.length - k; i++) { 
    testString = strarr.slice(i, i + k).join(''); 
    if (testString.length > longest.length) longest = testString; 
    } 
    return longest; 
} 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
0

哎呀,我有一個bug。沒有嘗試所有的測試用例。

偷偷摸摸的部分是在這裏:

sub = arr.slice(ix, ix + k).join(''); 

你把他們給你複製一個塊與slice法陣。這需要一個開始和結束值。因此,您從索引開始,並將其添加到k。然後你使用join方法將塊組合在一起作爲一個字符串。

現在很容易比較此字符串的長度和迄今爲止找到的最長的字符串的長度。我們從空字符串開始,所以第一個塊總是會更大。

如果你不知道關於slicejoin,它變得有點困難。

console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 3)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 0)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], -1)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 9)); 
 

 
function longest_consec(arr, k) { 
 
    var longest = ""; 
 
    var sub = ""; 
 

 
    if(k < 0 || k > arr.length) return ""; 
 

 
    for(var ix = 0; ix < arr.length; ix++) { 
 
     sub = arr.slice(ix, ix + k).join(''); 
 
     if(sub.length > longest.length) longest = sub; 
 
    } 
 

 
    return longest; 
 
}

+0

謝謝。我添加了一小塊,它工作。如果你能幫助我理解這一點,我將不勝感激。 –

+0

'function longestConsec(arr,k){0} {0} {0} var longest =「」; var sub =「」; ((arr.length == 0)||(k> arr.length)||(k <= 0)){ return'';對於(var ix = 0; ix longest.length)longest = sub; } 返回最長; }' –

0
<script> 
const Array = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; 
let bigArray = Array[0]; 
if(Array.length > 0){ 
    Array.forEach(value => { 
    if(value.length > bigArray.length){ 
     bigArray = value; 
    } 
    }); 
    console.log(bigArray); //abigail 
    //bigArray now contain the biggest string in the "Array" variable 
}else{ 
    //array is empty 
} 
</script> 
0

只是爲了好玩,以下是使用.slice().join()array .reduce() method在一起的版本。這基本上是一樣的其他.slice().join()答案,但使用.reduce()而不是顯式的for循環:

function longest_consec(arr, k) { 
 
    if (k > arr.length || k < 1) return ''; 
 
    return arr.reduce(function(prevLongest, c, i, a) { 
 
    var str = a.slice(i, i + k).join(''); 
 
    return str.length > prevLongest.length ? str : prevLongest; 
 
    }, ''); 
 
} 
 

 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 9)); 
 
console.log(longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 0));

還是我的代碼高爾夫上面使用箭頭功能重寫:

var longest_consec = (a, k) => 
 
    k>a.length||k<1?'':a.reduce((p,c,i,a)=>(c=a.slice(i,i+k).join('')).length>p.length?c:p,''); 
 

 
console.log(longest_consec(["zone","abigail","theta","form","libe","zas","theta","abigail"],2)); 
 
console.log(longest_consec(["zone","abigail","theta","form","libe","zas", "theta","abigail"],9)); 
 
console.log(longest_consec(["zone","abigail","theta","form","libe", "zas","theta","abigail"],0));