Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The title

You are given a matrix matrix with m rows and n columns. Please return all elements in the matrix in clockwise spiral order.

 

Example 1:

Input: * * * * matrix = [[1, 2, 3], [4 and 6], [7,8,9]] output:,2,3,6,9,8,7,4,5 [1]

Example 2:

Input: * * * * matrix = [[1, 2, 3, 4], [5,6,7,8], [9,10,11,12]] output:,2,3,4,8,12,11,10,9,5,6,7 [1]

 

Tip:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Their thinking

Idea 1

  1. The first step is to determine the empty array and return[]
  2. Infinite loop, access four edges, if out of bounds, that is finished access, exit
const spiralOrder = matrix= > {
    const res = [];

    // Define rows and columns
    // May be an empty array, so add a '? '
    const [m, n] = [matrix.length, matrix[0]? .length];if(! m || ! n)return res;

    // Initialize: left, right, up, down
    let [left, right, up, down] = [0, n - 1.0, m - 1];

    while (1) {
        // access top, left to right, after access, up++
        for (let j = left; j <= right; j++) res.push(matrix[up][j]);
        up++;
        // If the value of up is greater than that of Down, the access is complete and exit
        if (up > down) break;

        // Do the following
        for (let i = up; i <= down; i++) res.push(matrix[i][right]);
        right--;
        if (right < left) break;

        for (let j = right; j >= left; j--) res.push(matrix[down][j]);
        down--;
        if (down < up) break;

        for (let i = down; i >= up; i--) res.push(matrix[i][left]);
        left++;
        if (left > right) break;
    }

    return res;
};
Copy the code

The conventional method

 var spiralOrder = function (arr) {
  // The orientation defaults to the right
  let dir = 'right';
  // m rows and n columns
  let m = arr.length, n = arr[0].length;
  let y = 0, x = 0, r = [];
  // Cache the results
  let map = {};
  while (true) {
    let key = `${y}.${x}`;
    r.push(arr[y][x]);
    map[key] = 1;
    const canRight = x < n - 1 && !map[`${y}.${x + 1}`];
    const canDown = (y < m - 1) && !map[`${y + 1}.${x}`];
    const canLeft = x > 0 && !map[`${y}.${x - 1}`];
    const canTop = y > 0 && !map[`${y - 1}.${x}`];

    if (dir === 'right') {
      if (canRight) {
        x++
        continue
      }
      if (canDown) {
        dir = 'bottom';
        y++;
        continue
      }
      // There is no way out
      break;
    }

    if (dir === 'bottom') {
      if (canDown) {
        y++;
        continue
      }
      if (canLeft) {
        dir = 'left';
        x--;
        continue;
      }
      // There is no way out
      break;
    }

    if (dir === 'left') {
      if (canLeft) {
        x--;
        continue;
      }
      if (canTop) {
        y--;
        dir = 'top';
        continue;
      }
      // There is no way out
      break;
    }

    if (dir === 'top') {
      if (canTop) {
        y--;
        continue;
      }
      if (canRight) {
        x++;
        dir = 'right';
        continue;
      }
      // There is no way out
      break; }};return r;
}

Copy the code