Leetcode 54. Spiral Matrix [medium][java]

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

Example
在这里插入图片描述

Solution 1: Simulation

  1. Use two 1X4 arrays to mark the moving direction of the x and y index.
  2. Use a mXn array to mark if the number has been visited or not
  3. If the number has been visited, we should change the moving direction.
    Time Complexity: O(N), Space Complexity: O(N)
class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList();
        if(matrix == null || matrix.length == 0)
            return res;
        int m = matrix.length;
        int n = matrix[0].length;
        boolean[][] visited = new boolean[m][n];
        int[] dx = {0, 1, 0, -1};
        int[] dy = {1, 0, -1, 0};
        int x = 0;
        int y = 0;
        int di = 0;
        for(int i = 0; i < m*n; i++) {
            res.add(matrix[x][y]);
            visited[x][y] = true;
            int nextX = x + dx[di];
            int nextY = y + dy[di];
            if(0 <= nextY && nextY < n && 0 <= nextX && nextX < m && !visited[nextX][nextY]) {
                x = nextX;
                y = nextY;
            } else {
                di = (di+1) % 4;
                x += dx[di];
                y += dy[di];
            }
            
        }
        return res;
    }
}

Solution 2: layer by layer
Consideration

  1. We iterate each layer to iterate the matrix
  2. If the layer row length or column length is 0, the loop ends
    3. Be careful for the single row or single column layer situation

Time Complexity: O(N), space complexity: O(N)

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList();
        if(matrix == null || matrix.length == 0)
            return res;
        int m = matrix.length;
        int n = matrix[0].length;
        int x = 0;
        int y = 0;
        while(m > 0 && n > 0) {
            // for 1Xn array
            if(m == 1) {
                for(int i = 0; i < n; i++)
                    res.add(matrix[x][y++]);
                break;
            // for mX1 array
            } else if(n == 1) {
                for(int i= 0; i < m; i++)
                    res.add(matrix[x++][y]);
                break;
            } else {

                for(int i = 0; i < n-1; i++) {
                    res.add(matrix[x][y++]);
                }
                for(int i = 0; i < m-1; i++) {
                    res.add(matrix[x++][y]);
                }
                for(int i = 0; i < n-1; i++) {
                    res.add(matrix[x][y--]);
                }
                for(int i = 0; i < m-1; i++) {
                    res.add(matrix[x--][y]);
                }
                x+=1;
                y+=1;
                m-=2;
                n-=2;
            }

        }
        return res;
    }
}

References

  1. https://leetcode.com/problems/spiral-matrix/solution/
  2. https://www.cnblogs.com/springfor/p/3887890.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值