Codewars Kata 上的递归问题 - Snail Trail

对编码非常陌生,所以请多多包涵。我正在尝试在 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);

}


慕斯王
浏览 181回答 3
3回答

蝴蝶不菲

这是一种不会改变 input 的非递归方法array。它通过跟踪左上角坐标和螺旋线x, y的大小来工作。nsnail = function(array) {&nbsp; const { length } = array;&nbsp; const result = [];&nbsp; let x = 0;&nbsp; let y = 0;&nbsp; let n = length;&nbsp; while (n > 0) {&nbsp; &nbsp; // travel right from top-left of spiral&nbsp; &nbsp; for (let i = x; i < x + n; ++i) result.push(array[y][i]);&nbsp; &nbsp; // shrink spiral and move top of spiral down&nbsp; &nbsp; n--; y++;&nbsp; &nbsp; // travel down from top-right of spiral&nbsp; &nbsp; for (let i = y; i < y + n; ++i) result.push(array[i][x + n]);&nbsp; &nbsp; // travel left from bottom-right of spiral&nbsp; &nbsp; for (let i = x + n - 1; i >= x; --i) result.push(array[y + n - 1][i]);&nbsp; &nbsp; // shrink spiral&nbsp; &nbsp; n--;&nbsp; &nbsp; // travel up from bottom-left of spiral&nbsp; &nbsp; for (let i = y + n - 1; i >= y; --i) result.push(array[i][x]);&nbsp; &nbsp; // move left of spiral right&nbsp; &nbsp; x++;&nbsp; }&nbsp; return result;}console.log(snail([[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]));

MMTTMM

一种选择是在 inside 定义另一个函数snail,它递归地调用自身,同时定义outputarrayinside snail。这样,outputarray不会暴露给外部范围,但递归函数仍然可以看到它。另请注意,它splice返回一个数组,所以现在,您outputarray由一个数组数组组成。改为push展开来修复它,使outputarray成为一个数字数组:const input = [&nbsp; [1, 2, 3, 4],&nbsp; [12, 13, 14, 5],&nbsp; [11, 16, 15, 6],&nbsp; [10, 9, 8, 7]];const snail = (array) => {&nbsp; const outputarray = [];&nbsp; const iter = () => {&nbsp; &nbsp; if (array.length == 0) {&nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; var n = array[0].length - 1;&nbsp; &nbsp; for (var i = 0; i <= n; i++) {&nbsp; &nbsp; &nbsp; outputarray.push(...array[0].splice(0, 1));&nbsp; &nbsp; }&nbsp; &nbsp; for (var i = 1; i <= n; i++) {&nbsp; &nbsp; &nbsp; outputarray.push(...array[i].splice(n, 1));&nbsp; &nbsp; }&nbsp; &nbsp; for (var i = n - 1; i >= 0; i--) {&nbsp; &nbsp; &nbsp; outputarray.push(...array[n].splice(i, 1));&nbsp; &nbsp; }&nbsp; &nbsp; for (var i = n - 1; i > 0; i--) {&nbsp; &nbsp; &nbsp; outputarray.push(...array[i].splice(0, 1));&nbsp; &nbsp; }&nbsp; &nbsp; array.pop();&nbsp; &nbsp; array.shift();&nbsp; &nbsp; iter(array);&nbsp; };&nbsp; iter(array);&nbsp; return outputarray;}console.log(snail(input));

跃然一笑

这可能不符合 kata 的规则(或精神?),但是,您可以将它们粘在一起并分类。function snail(trail) {&nbsp; const numeric = (a, b) => a - b&nbsp; const gather = (items, item) => items.push(parseInt(item, 10)) && items&nbsp; const inline = (route, points) => points.reduce(gather, route) && route&nbsp; const order = paths => paths.reduce(inline, []).sort(numeric)&nbsp; return order(trail)}const trail = [&nbsp; &nbsp; [1, 2, 3, 4],&nbsp;&nbsp; &nbsp; [12, 13, 14, 5],&nbsp;&nbsp; &nbsp; [11, 16, 15, 6],&nbsp;&nbsp; &nbsp; [10, 9, 8, 7]]console.log(JSON.stringify(snail(trail)))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript