codewars贪吃蛇算法题目

有这样一个题目:

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]

For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]

This image will illustrate things more clearly:

NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.

NOTE 2: The 0x0 (empty matrix) is represented as [[]]

题目意思是,给出一个N*N的二维数组,要求输出一个顺时针的一维数组,0*0的和1*1的直接原样一维数组返回就行了。

下面是我的解法:

snail = function (array) {
      // enjoy
      let len = array.length;
      console.log(len);
      if (array.length == 1) {
        return array[0];
      } else {
        let i = 0, j = len - 1;
        let rowStart = 1, rowEnd = len - 1, colStart = 0, colEnd = len - 2;
        let direction = 'down';
        for (let index = 0; index < len * (len - 1); index++) {
          console.log(direction, 'direction');
          switch (direction) {
            case 'down':
              i++;
              if (i >= rowEnd) {
                i = rowEnd;
                rowEnd--;
                console.log('down', i, rowEnd);
                direction = 'left';
              }
              break;
            case 'left':
              j--;
              if (j <= colStart) {
                j = colStart;
                colStart++;
                console.log('left');
                direction = 'up';
              }
              break;
            case 'up':
              i--;
              if (i <= rowStart) {
                i = rowStart;
                rowStart++;
                console.log('up');
                direction = 'right';
              }
              break;
            case 'right':
              j++;
              if (j >= colEnd) {
                j = colEnd;
                colEnd--;
                console.log('right');
                direction = 'down';
              }
              break;
          }
          console.log(array[i][j], i, j);
          array[0].push(array[i][j]);
        }

      }
      return array[0];
    }

  虽然感觉啰嗦,但是勉强实现了。

  下面看看得分最高的选手的代码:

snail = function (array) {
      var result; 
while (array.length) { result = (result ? result.concat(array.shift()) : array.shift()); for (var i = 0; i < array.length; i++) { result.push(array[i].pop()); } row.result = result.concat((array.pop() || []).reverse()); for (var i = array.length - 1; i >= 0; i--) { result.push(array[i].shift()); } } return result; }

代码简单,清晰明了,充分调用了数组的各种方法,pop、shift、reverse等。

还有更简单的写法,原理大同小异,可能广大网友没怎么往下看或者觉得上面选手的代码更容易读懂的原因吧,得分不高,但是代码更是简练到了极致,欣赏一下代码的艺术吧。

const snail = function (array) {
      const list = [];
      while (array.length) {
        list.push(...array.shift(), ...array.map(row => row.pop()));
        array.reverse().map(row => row.reverse());
      }
      return list;
    }

that's all,  thanks!

 

posted @ 2019-05-23 10:59  liujiekun  阅读(505)  评论(0编辑  收藏  举报