螺旋矩阵(Java)

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

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

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

package com.loo;

import java.util.ArrayList;
import java.util.List;

public class CircleOrder {

    public static void main(String[] args) {
        int[][] arr = new int[][] {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12}
        };
        List<Integer> list = getCircleOrder2(arr);//getCircleOrder(arr);
        for (int i=0;i<list.size();i++) {
            System.out.print(list.get(i));
        }
    }
    
/*
可以模拟螺旋矩阵的路径。初始位置是矩阵的左上角,初始方向是向右,当路径超出界限或者进入之前访问过的位置时,则顺时针旋转,进入下一个方向。
判断路径是否进入之前访问过的位置需要使用一个与输入矩阵大小相同的辅助矩阵 visited,其中的每个元素表示该位置是否被访问过。当一个元素被访问时,将 visited 中的对应位置的元素设为已访问。
由于矩阵中的每个元素都被访问一次,因此路径的长度即为矩阵中的元素数量,当路径的长度达到矩阵中的元素数量时即为完整路径,将该路径返回。时间复杂度:O(mn),其中 m 和 n 分别是输入矩阵的行数和列数。矩阵中的每个元素都要被访问一次。空间复杂度:O(mn)。需要创建一个大小为 m×n 的矩阵 visited 记录每个位置是否被访问过。
*/
    public static List<Integer> getCircleOrder(int[][] matrix) {
        List<Integer> order = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return order;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        boolean[][] visited = new boolean[rows][columns];
        int total = rows * columns;
        int[][] directions = {{0,1} , {1,0} , {0,-1} , {-1,0}};
        int directionsIndex = 0;
        int row = 0;
        int column = 0;
        for (int i=0;i<total;i++) {
            order.add(matrix[row][column]);
            visited[row][column] = true;
            int nextRow = row + directions[directionsIndex][0];
            int nextColumn = column + directions[directionsIndex][1];
            if (nextRow<0 || nextRow>=rows || nextColumn<0 || nextColumn>=columns || visited[nextRow][nextColumn]) {
                directionsIndex = (directionsIndex+1)%4;
            }
            row += directions[directionsIndex][0];
            column += directions[directionsIndex][1];
        }
        return order;
    }
    
/*
按照顺时针方向遍历。时间复杂度:O(mn),其中 m 和 n 分别是输入矩阵的行数和列数。矩阵中的每个元素都要被访问一次。空间复杂度:O(1)。除了输出数组以外,空间复杂度是常数。
*/
    public static List<Integer> getCircleOrder2(int[][] matrix) {
        List<Integer> order = new ArrayList<Integer>();
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return order;
        }
        int rows = matrix.length;
        int columns = matrix[0].length;
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left<=right && top<=bottom) {
            for (int c=left;c<=right;c++) {
                order.add(matrix[top][c]);
            }
            for (int r=top+1;r<=bottom;r++) {
                order.add(matrix[r][right]);
            }
            if (left<right && top <bottom) {
                for (int c=right-1;c>left;c--) {
                    order.add(matrix[bottom][c]);
                }
                for (int r=bottom;r>top;r--) {
                    order.add(matrix[r][left]);
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值