Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

打印一个螺旋式矩阵 #217

Open
Nasuke opened this issue Nov 4, 2022 · 4 comments
Open

打印一个螺旋式矩阵 #217

Nasuke opened this issue Nov 4, 2022 · 4 comments

Comments

@Nasuke
Copy link
Contributor

Nasuke commented Nov 4, 2022

给定一个整数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;
}
@Sunny-117
Copy link
Owner

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

@Achetto
Copy link

Achetto commented Dec 2, 2022

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;
};

@Bbbtt04
Copy link

Bbbtt04 commented Mar 24, 2023

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
};

@Simonduya
Copy link

 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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants