程序员社区

LeetCode-54-螺旋矩阵

LeetCode-54-螺旋矩阵

54. 螺旋矩阵

难度中等772收藏分享切换为英文接收动态反馈

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

示例 1:

LeetCode-54-螺旋矩阵插图
img
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

LeetCode-54-螺旋矩阵插图1
img
输入: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

LeetCode-54-螺旋矩阵插图2
image-20210514101253284
LeetCode-54-螺旋矩阵插图3
image-20210514101421609
LeetCode-54-螺旋矩阵插图4
image-20210514101559321
class Solution {
    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> ans = new ArrayList<>();
        if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
            return ans;
        }
        int a = 0;
        int b = 0;
        int c = matrix.length - 1;
        int d = matrix[0].length - 1;
        while (a <= c && b <= d) {
            addEdge(matrix, a++, b++, c--, d--, ans);
        }
        return ans;
    }

    public static void addEdge(int[][] m, int a, int b, int c, int d, List<Integer> ans) {
        if (a == c) {//为单行
            for (int i = b; i <= d; i++) {
                ans.add(m[a][i]);
            }
        } else if (b == d) {//单列
            for (int i = a; i <= c; i++) {
                ans.add(m[i][b]);
            }
        } else {//长方形,分解四块
            int curC = b;
            int curR = a;
            while (curC != d) {
                ans.add(m[a][curC]);
                curC++;
            }
            while (curR != c) {
                ans.add(m[curR][d]);
                curR++;
            }
            while (curC != b) {
                ans.add(m[c][curC]);
                curC--;
            }
            while (curR != a) {
                ans.add(m[curR][b]);
                curR--;
            }
        }
    }
}
LeetCode-54-螺旋矩阵插图5
image-20210514101719279
  • swift
class Solution {
   func spiralOrder(_ matrix: [[Int]]) -> [Int] {
        var res = [Int]()

        guard matrix.count > 0 else {
            return res
        }

        var startX = 0
        var endX = matrix.count - 1
        var startY = 0
        var endY = matrix[0].count - 1
         
        while true {
            for i in startY...endY {
                res.append(matrix[startX][i])
            }
            startX += 1
            if startX > endX {
                break
            }

            for i in startX...endX {
                res.append(matrix[i][endY])
            }

            endY -= 1
            if startY > endY {
                break
            }

            for i in stride(from: endY, through: startY, by: -1) {
                res.append(matrix[endX][i])
            }
            endX -= 1
            if startX > endX {
                break
            }

            for i in stride(from:endX, through: startX, by: -1) {
                res.append(matrix[i][startY])
            }
            startY += 1
            if startY > endY {
                break
            }
        }
        return res

    }
}
赞(0) 打赏
未经允许不得转载:IDEA激活码 » LeetCode-54-螺旋矩阵

一个分享Java & Python知识的社区