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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

功能保持超時

功能保持超時

達(dá)令說 2023-01-06 10:37:32
這就是問題:完成函數(shù) splitPairs,將輸入字符串拆分為字符對。如果輸入字符串的長度為奇數(shù),則應(yīng)將最后一對中缺失的第二個字符替換為下劃線 _。請注意,一個空字符串應(yīng)該使您的函數(shù)產(chǎn)生一個空數(shù)組。這是我的代碼(它一直超時):function splitPairs(input) {  let inputArray = input.split('');  let result = [];        if (inputArray.length % 2 !== 0) {    for (let i = 0; i < inputArray.length; i + 2) {      let pair = inputArray[i] + inputArray[i+1];      //push that onto the result array      result.push(pair);    }    result.push(inputArray[inputArray.length - 1] + '_');        } else {    for (let i = 0; i < inputArray.length; i + 2) {      let pair = inputArray[i] + inputArray[i+1];      result.push(pair);    }  }  return result;}我做錯了什么,解決這個問題的正確方法是什么?如果我可以自己編寫解決方案會更好但是我可以使用幫助來了解我應(yīng)該使用什么方法來解決它
查看完整描述

4 回答

?
夢里花落0921

TA貢獻(xiàn)1772條經(jīng)驗 獲得超6個贊

超時是因為您沒有遞增ii + 2計算新值但不將其分配到任何地方。你可以i通過做i += 2which 的簡寫來更新i = i + 2



查看完整回答
反對 回復(fù) 2023-01-06
?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗 獲得超0個贊

您需要使用i+=2. 此外,解決方案中存在一些錯誤:


function splitPairs(input) {

  let inputArray = input.split('');

  let result = [];

  if(!inputArray)

    return result;

  if (inputArray.length % 2 !== 0) {

    for (let i = 0; i < inputArray.length-1; i+=2) {

      let pair = inputArray[i] + inputArray[i+1];

      result.push(pair);

    }

    result.push(inputArray[inputArray.length - 1] + '_');    

  } else {

    for (let i = 0; i < inputArray.length; i += 2) {

      let pair = inputArray[i] + inputArray[i+1];

      result.push(pair);

    }

  }

  return result;

}


console.log(splitPairs(""));


console.log(splitPairs("abcd"));


console.log(splitPairs("abcde"));


評論中提到的一個更簡單的解決方案(一個循環(huán))是:


function splitPairs(input) {

  let inputArray = input.split('');

  let result = [];

  if(!inputArray)

    return result;

  let odd = (inputArray.length % 2 !== 0);

  let len = (odd) ? inputArray.length-1 : inputArray.length;

  for (let i = 0; i < len; i+=2) {

    let pair = inputArray[i] + inputArray[i+1];

    result.push(pair);

  }

  if(odd)

    result.push(inputArray[inputArray.length - 1] + '_');    

  return result;

}


console.log(splitPairs(""));


console.log(splitPairs("abcd"));


console.log(splitPairs("abcde"));


查看完整回答
反對 回復(fù) 2023-01-06
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超10個贊

您可以使用一個(兩個)襯墊

var result=str.split(/(..)/).filter(v=>v)
if (result[result.length-1].length==1) result[result.length-1]+="_"


查看完整回答
反對 回復(fù) 2023-01-06
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗 獲得超3個贊

你可以這樣做


function splitPairs(input) {

  return input.split('').map((c, i) => {

    if (i % 2 !== 0) return;

    if (input[i+1]) {

      return input[i] + input[i+1];

    }    


    return input[i] + '_';

  }).filter(pair => pair);

}


查看完整回答
反對 回復(fù) 2023-01-06
  • 4 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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