Leetcode-Spiral Matrix

本文介绍了一种矩阵螺旋遍历算法的两种实现方案。第一种使用布尔矩阵跟踪已访问元素,第二种通过更新行列边界简化判断逻辑,提高了效率。

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

Solution:

 1 public class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         int xLen = matrix.length;
 5         if (xLen==0) return res;
 6         int yLen = matrix[0].length;
 7         if (yLen==0) return res;
 8 
 9         int[] x = new int[]{0,1,0,-1};
10         int[] y = new int[]{1,0,-1,0};
11         boolean[][] printed = new boolean[xLen][yLen];
12         for (int i=0;i<xLen;i++)
13             Arrays.fill(printed[i],false);
14         int direction = 0;
15         int curX = 0, curY=0;        
16         for (int i=0;i<xLen*yLen;i++){
17             res.add(matrix[curX][curY]);
18             printed[curX][curY] = true;
19             int nextX = curX+x[direction];
20             int nextY = curY+y[direction]; 
21             //Determin the availability of next point.
22             if (nextX>=xLen || nextX<0 || nextY>=yLen || nextY<0 || printed[nextX][nextY]){
23                 direction = (direction+1)%4;
24                 nextX = curX+x[direction];
25                 nextY = curY+y[direction];
26             }
27             curX = nextX;
28             curY = nextY;
29         }
30         return res;        
31     }
32 }

Solution 2:

We actually does not need the matrix to record the visited element, because each time the direction is changed, one row or one col becomes unavailable. We only need to record and update the start and end of the available rows and clos.

 1 public class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         List<Integer> res = new ArrayList<Integer>();
 4         int xLen = matrix.length;
 5         if (xLen==0) return res;
 6         int yLen = matrix[0].length;
 7         if (yLen==0) return res;
 8 
 9         int[] x = new int[]{0,1,0,-1};
10         int[] y = new int[]{1,0,-1,0};      
11         int direction = 0;
12         int curX = 0, curY=0;        
13         int rowStart = 0, rowEnd = xLen-1, colStart=0, colEnd=yLen-1;
14 
15         for (int i=0;i<xLen*yLen;i++){
16             res.add(matrix[curX][curY]);
17             int nextX = curX+x[direction];
18             int nextY = curY+y[direction]; 
19             //Determin the availability of next point.
20             if (nextX>rowEnd || nextX<rowStart || nextY>colEnd || nextY<colStart){
21                 direction = (direction+1)%4;
22                 nextX = curX+x[direction];
23                 nextY = curY+y[direction];
24                 //Update the availability of rows and cols according the direction change.
25                 if (direction==1) rowStart++;
26                 if (direction==2) colEnd--;
27                 if (direction==3) rowEnd--;
28                 if (direction==0) colStart++;
29             }
30             curX = nextX;
31             curY = nextY;
32         }
33         return res;        
34     }
35 }

 

转载于:https://www.cnblogs.com/lishiblog/p/4102804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值