亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

Codewars Kata 上的遞歸問題 - Snail Trail

Codewars Kata 上的遞歸問題 - Snail Trail

慕斯王 2022-01-13 17:30:28
對編碼非常陌生,所以請多多包涵。我正在嘗試在 Codewars 上解決這個 Kata:https ://www.codewars.com/kata/snail/train/javascript基本上給定一個數組[     [1, 2, 3, 4],     [12,13,14,5],     [11,16,15,6],     [10,9, 8, 7]];它會返回[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]。圍繞矩陣外部和內部的蝸牛軌跡。我只是解決矩陣是 nxn 的情況,其中 n > 1 并且現在是偶數。我通過在函數外部聲明 outputarray 來使其工作,但我希望在函數內聲明該數組,因此包含以下行: var outputarray = outputarray || [];不知道我哪里出錯了。snail = function(array) {  if (array.length == 0) {    return outputarray  }  var n = array[0].length - 1;  var outputarray = outputarray || [];  for (var i = 0; i <= n; i++) {    outputarray.push(array[0].splice(0, 1));  }  for (var i = 1; i <= n; i++) {    outputarray.push(array[i].splice(n, 1));  }  for (var i = n - 1; i >= 0; i--) {    outputarray.push(array[n].splice(i, 1));  }  for (var i = n - 1; i > 0; i--) {    outputarray.push(array[i].splice(0, 1));  }  array.pop();  array.shift();  snail(array);}
查看完整描述

3 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

這是一種不會改變 input 的非遞歸方法array。它通過跟蹤左上角坐標和螺旋線x, y的大小來工作。n


snail = function(array) {

  const { length } = array;

  const result = [];

  let x = 0;

  let y = 0;

  let n = length;


  while (n > 0) {

    // travel right from top-left of spiral

    for (let i = x; i < x + n; ++i) result.push(array[y][i]);


    // shrink spiral and move top of spiral down

    n--; y++;


    // travel down from top-right of spiral

    for (let i = y; i < y + n; ++i) result.push(array[i][x + n]);


    // travel left from bottom-right of spiral

    for (let i = x + n - 1; i >= x; --i) result.push(array[y + n - 1][i]);


    // shrink spiral

    n--;


    // travel up from bottom-left of spiral

    for (let i = y + n - 1; i >= y; --i) result.push(array[i][x]);


    // move left of spiral right

    x++;

  }


  return result;

}


console.log(snail([[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]));



查看完整回答
反對 回復 2022-01-13
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

一種選擇是在 inside 定義另一個函數snail,它遞歸地調用自身,同時定義outputarrayinside snail。這樣,outputarray不會暴露給外部范圍,但遞歸函數仍然可以看到它。


另請注意,它splice返回一個數組,所以現在,您outputarray由一個數組數組組成。改為push展開來修復它,使outputarray成為一個數字數組:


const input = [

  [1, 2, 3, 4],

  [12, 13, 14, 5],

  [11, 16, 15, 6],

  [10, 9, 8, 7]

];


const snail = (array) => {

  const outputarray = [];

  const iter = () => {

    if (array.length == 0) {

      return

    }

    var n = array[0].length - 1;

    for (var i = 0; i <= n; i++) {

      outputarray.push(...array[0].splice(0, 1));

    }

    for (var i = 1; i <= n; i++) {

      outputarray.push(...array[i].splice(n, 1));

    }

    for (var i = n - 1; i >= 0; i--) {

      outputarray.push(...array[n].splice(i, 1));

    }

    for (var i = n - 1; i > 0; i--) {

      outputarray.push(...array[i].splice(0, 1));

    }

    array.pop();

    array.shift();

    iter(array);

  };

  iter(array);

  return outputarray;

}


console.log(snail(input));


查看完整回答
反對 回復 2022-01-13
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

這可能不符合 kata 的規則(或精神?),但是,您可以將它們粘在一起并分類。


function snail(trail) {

  const numeric = (a, b) => a - b

  const gather = (items, item) => items.push(parseInt(item, 10)) && items

  const inline = (route, points) => points.reduce(gather, route) && route

  const order = paths => paths.reduce(inline, []).sort(numeric)


  return order(trail)

}


const trail = [

    [1, 2, 3, 4], 

    [12, 13, 14, 5], 

    [11, 16, 15, 6], 

    [10, 9, 8, 7]

]


console.log(JSON.stringify(snail(trail)))


查看完整回答
反對 回復 2022-01-13
  • 3 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號