Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

打印一个螺旋式矩阵

Nasuke opened this issue · comments

给定一个整数N 返回一个NXN的螺旋式矩阵

--- examples

//matrix(3)
[[1,2,3],
[8,9,4],
[7,6,5]]
//matrix(4)
[[1,2,3,4],
[12,13,14,5],
[11,16,15,6],
[10,9,8,7]]

这就是返回的顺时针的螺旋式矩阵啦~~
以下是代码部分:

function matrix(n) {
    const results = [];
    for (let i = 0; i < n; i++) {
        results.push([]);
    }
    let counter = 1;
    let startColumn = 0;
    let endColumn = n - 1;
    let startRow = 0;
    let endRow = n - 1;
    while (startColumn <= endColumn && startRow <= endRow) {
        // Top row
        for (let i = startColumn; i <= endColumn; i++) {
            results[startRow][i] = counter;
            counter++;
        }
        startRow++;
        // Right column
        for (let i = startRow; i <= endRow; i++) {
            results[i][endColumn] = counter;
            counter++;
        }
        endColumn--;
        // Bottom row
        for (let i = endColumn; i >= startColumn; i--) {
            results[endRow][i] = counter;
            counter++;
        }
        endRow--;
        // start column
        for (let i = endRow; i >= startRow; i--) {
            results[i][startColumn] = counter;
            counter++;
        }
        startColumn++;
    }
    return results;
}

去提pr呀,写到issue里面发现不了

var spiralOrder = function(matrix) {
  if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return [];
  let left = 0;
  let right = matrix[0].length - 1;
  let up = 0;
  let down = matrix.length - 1;
  let res = [];
  let numele = matrix.length * matrix[0].length;
  while (left <= right && up <= down) {
    for (let i = left; i <= right && numele >= 1; i++) {
      res.push(matrix[up][i]);
      numele--
    }
    up++;
    for (let i = up; i <= down && numele >= 1; i++) {
      res.push(matrix[i][right]);
      numele--
    }
    right--;
    for (let i = right; i >= left && numele >= 1; i--) {
      res.push(matrix[down][i]);
      numele--
    }
    down--;
    for (let i = down; i >= up && numele >= 1; i--) {
      res.push(matrix[i][left]);
      numele--
    }
    left++;
  }
  return res;
};
var spiralOrder = function(matrix) {
    let n = matrix.length,m = matrix[0].length;
    let res = [];
    let left = 0, top = 0 , right = m - 1, bottom = n - 1;
    while(left < right && top < bottom) {
        for(let i = left ; i < right ; i ++) res.push(matrix[top][i])
        for(let i = top ; i < bottom ; i ++) res.push(matrix[i][right])
        for(let i = right ; i > left ; i --) res.push(matrix[bottom][i])
        for(let i = bottom ; i > top ; i --) res.push(matrix[i][left])
        left ++
        top ++
        right --
        bottom --
    }
    if(top === bottom) {
        for(let i = left ; i <= right ; i ++) res.push(matrix[top][i])
    } else if(left === right){
        for(let i = top ; i <= bottom ; i ++) res.push(matrix[i][left])
    }
    return res
};
 function generateMatrix(n) {
        const arr = new Array(n).fill(0).map(() => new Array(n).fill(0));
        // 奇数单独设置中间元素
        if (n % 2) {
          const mid = Math.floor(n / 2);
          arr[mid][mid] = n * n;
        }
        // 循环圈数
        let loop = Math.floor(n / 2);
        // 用于控制每次循环遍历边的长度
        let offset = 1;
        let num = 1;

        let startX = 0;
        let startY = 0;
        while (loop--) {
          for (let k = startY; k < n - offset; k++) {
            arr[startX][k] = num++;
          }
          for (let k = startX; k < n - offset; k++) {
            arr[k][n - offset] = num++;
          }
          for (let k = n - offset; k > startX; k--) {
            arr[n - offset][k] = num++;
          }
          for (let k = n - offset; k > startY; k--) {
            arr[k][startY] = num++;
          }
          startX++;
          startY++;
          offset++;
        }
        return arr;
      }
      generateMatrix(7);