Codewars Kata上的递归问题-Snail Trail

时间:2019-11-27 21:14:47

标签: javascript arrays recursion

编码非常陌生,请耐心等待。我正在尝试在Codewars上解决此问题: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]

蜗牛的踪迹绕着矩阵的外部向内盘旋。

我正在解决矩阵为n x n的情况,其中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);
}

6 个答案:

答案 0 :(得分:1)

一个选择是在some_div_selection.append("button").text("+").on("click", console.log("You clicked me")); 内定义另一个函数,该函数递归调用自身,同时在some_div_selection.append("button").attr("onclick", "foobar()").text("+") 内定义snail。这样,outputarray不会暴露给外部作用域,但是递归函数仍然可以看到它。

还要注意,snail返回一个数组,所以现在,您的outputarray由一个数组组成。改为传播到splice中进行修复,以使outputarray成为数字数组:

push

答案 1 :(得分:0)

这是一种不递归的方法,不会改变输入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]]));

答案 2 :(得分:0)

您可以为左,右,上,下索引取一些边界并循环,直到没有可用的索引为止。

function snail(array) {
    var upper = 0,
        lower = array.length - 1,
        left = 0,
        right = array[0].length - 1,
        i = upper,
        j = left,
        result = [];

    while (true) {
        if (upper++ > lower) break;

        for (; j < right; j++) result.push(array[i][j]);
        if (right-- < left) break;

        for (; i < lower; i++) result.push(array[i][j]);
        if (lower-- < upper) break;

        for (; j > left; j--) result.push(array[i][j]);
        if (left++ > right) break;

        for (; i > upper; i--) result.push(array[i][j]);
    }

    result.push(array[i][j]);
    return result;
}

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

答案 3 :(得分:0)

尝试一下:

Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class TCPControl
    Public Client As TcpClient
    Public DataStream As StreamWriter

    Public Sub New(Host As String, Port As Integer)
        ' client
        Client = New TcpClient(Host, Port)
        DataStream = New StreamWriter(Client.GetStream)
    End Sub

    Public Sub Send(Data)
        DataStream.Write(Data)
        DataStream.Flush()
    End Sub
End Class

答案 4 :(得分:0)

这可能不符合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)))

答案 5 :(得分:0)

这是我的两分钱,使用习惯递归:

function f(A){
  return A.length > 1 ? A.splice(0,1)[0].concat(
    f(A[0].map((c, i) => A.map(r => r[i])).reverse())) : A[0]
}

var A = [[ 1, 2, 3, 4], [12,13,14, 5], [11,16,15, 6], [10, 9, 8, 7]]

console.log(JSON.stringify(f(A)))