54. 螺旋矩阵(JavaScript)

题目描述:给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

解法一:

思路:

1. 循环遍历矩阵,循环终止条件:遍历元素个数count等于矩阵元素个数rows * cols;

2. 遍历方向为螺旋方向:right -> down -> left -> up,方向变化条件:碰到第一个访问标志位visited为真或矩阵边界,改变遍历方向。

var spiralOrder = function (matrix) {
  if (matrix.length == 0)
    return [];
  var res = [];
  var visited = [];
  var dir = "right";
  var rows = matrix.length;
  var cols = matrix[0].length;
  for (var i = 0; i < rows; i++) {
    visited[i] = [];
    for (var j = 0; j < cols; j++)
      visited[i][j] = false;
  }
  var visit = rows * cols;
  var i = 0, j = 0, count = 0;
  while (count < rows * cols) {
    switch (dir) {
      case "right":
        for (var k = j; k < cols; k++) {
          if (visited[i] && visited[i][k] == false) {
            res[count++] = matrix[i][k];
            visited[i][k] = true;
            if (k == cols - 1) {
              i++;
              j = k;
              dir = "down";
              break;
            }
          }
          else {
            i++;
            j = k - 1;
            dir = "down";
            break;
          }
        }
        break;
      case "down":
        for (var k = i; k < rows; k++) {
          if (visited[k] && visited[k][j] == false) {
            res[count++] = matrix[k][j];
            visited[k][j] = true;
            if (k == rows - 1) {
              i = k;
              j--;
              dir = "left";
              break;
            }
          }
          else {
            i = k - 1;
            j--;
            dir = "left";
            break;
          }
        }
        break;
      case "left":
        for (var k = j; k >= 0; k--) {
          if (visited[i] && visited[i][k] == false) {
            res[count++] = matrix[i][k];
            visited[i][k] = true;
            if (k == 0) {
              i--;
              j = k;
              dir = "up";
              break;
            }
          }
          else {
            i--;
            j = k + 1;
            dir = "up";
            break;
          }
        }
        break;
      case "up":
        for (var k = i; k >= 0; k--) {
          if (visited[k] && visited[k][j] == false) {
            res[count++] = matrix[k][j];
            visited[k][j] = true;
            if (k == 0) {
              i = k;
              j++;
              dir = "right";
              break;
            }
          }
          else {
            i = k + 1;
            j++;
            dir = "right";
            break;
          }
        }
        break;
    }
  }
  return res;
}

 

解法二:

思路: 

1. 设置4个变量top,right,bottom,left分别表示矩阵的上、右、下、左边界,当left大于right或top大于bottom是结束循环;

2. 循环体内,按右 -> 下 -> 左 ->上的顺序进行遍历,并对相应的top、right、bottom、left值进行处理,改变边界值,直到满足循环结束条件。

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
   var spiralOrder = function (matrix) {
       if(matrix.length == 0)
        return [];
       var top = 0, left = 0, right = matrix[0].length - 1, bottom = matrix.length - 1;
       var res = [];
       var index = 0;
       while(top <= bottom && left <= right) {
         for(var i = left; i <= right; i++)
           res[index++] = matrix[top][i];
         top++;
         if(top > bottom)
          break;
         for(var i = top; i <= bottom; i++) 
          res[index++] = matrix[i][right];
         right--;
         if(right < left)
          break;
         for(var i = right; i >= left; i--)
          res[index++] = matrix[bottom][i];
         bottom--;
         if(bottom < top)
          break;
         for(var i = bottom; i >= top; i--)
          res[index++] = matrix[i][left];
         left++;
         if(left > right)
          break;
       }
       return res;
     };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值