螺旋矩阵的遍历

54. 螺旋矩阵

给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

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

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100
  • class Solution {
        public List<Integer> spiralOrder(int[][] matrix) {
            List<Integer>res = new ArrayList<Integer>();
            if(matrix==null||matrix.length==0||matrix[0].length==0){
                return res;
            }
            int rows = matrix.length;
            int columns = matrix[0].length;
            int total = rows*columns;
            int row=0,column=0;
            boolean[][]seen = new boolean[rows][columns];
            int[][]directions = {{0,1},{1,0},{0,-1},{-1,0}};
            int directionindex = 0;
            for(int i=0;i<total;i++){
                res.add(matrix[row][column]);
                seen[row][column] = true;
                int next_row = row + directions[directionindex][0];
                int next_column = column + directions[directionindex][1];
                if(next_row<0||next_row>=rows||next_column<0||next_column>=columns||seen[next_row][next_column]){
                    directionindex = (directionindex+1)%4;
                }
                row += directions[directionindex][0];
                column+= directions[directionindex][1];
            }
            return res;
        }
    }

    遍历次数为矩阵的元素个数,因此,再用一个数组seen来表示是否遍历过该元素,每次遍历完一个元素时,将这个位置设定为true,如果该元素遍历过,则调整方向,方向数组direction为一个4*2的数组,4行表示上下左右四个方向,两列表示行变换和列变换,列表示方向的递增递减不动,分别用1,-1,0表示,当我们遍历完第i行,第j列元素时,我们将下个元素的行和列表示出来,next_row = row + directions[directionindex][0],next_column = column + directions[directionindex][1];

  • 如果下个元素的行列越界,或者是下个要访问的位置已经被设定为true,那么我们将它+1再模4。

  • 将行和列更新,进入下一次循环

  • 循环total次后,返回

补充一下n*n形状的螺旋矩阵,输入一个n,返回n行n列的螺旋矩阵,例如,输入5,返回

下面是代码:

import java.util.Scanner;
public class dwa {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][]res = new int[n][n];
        //行列都为n
        int totals = n*n;
        int[][]direction = {{0,1},{1,0},{0,-1},{-1,0}};
        int directionIndex = 0;
        int cnt = 0;
        int row = 0;
        int column = 0;
        boolean[][]seen = new boolean[n][n];
        for(int i=0;i<totals;i++){
            res[row][column] = ++cnt;
            seen[row][column] = true;
            int next_row = row + direction[directionIndex][0];
            int next_column = column + direction[directionIndex][1];
            if(next_row<0||next_row>=n||next_column<0||next_column>=n||seen[next_row][next_column]){
                directionIndex = (directionIndex+1)%4;
            }
            row += direction[directionIndex][0];
            column += direction[directionIndex][1];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                System.out.print(res[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

  • 38
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值