【中等】54. 螺旋矩阵

题目

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

解题

func spiralOrder(matrix [][]int) []int {
    
    x0, y0, x1, y1 := 0, 0, len(matrix)-1, len(matrix[0])-1
    result := []int{}

    for x0<=x1 && y0<=y1 {
        for i:=y0;i<=y1;i++{
           result = append(result, matrix[x0][i])
        }
        x0++
        if x0>x1{
            break
        }

        for i:=x0;i<=x1;i++{
            result = append(result, matrix[i][y1])
        }
        y1--
        if y0>y1{
            break
        }


        for i:=y1;i>=y0;i--{
            result = append(result, matrix[x1][i])
        }
        x1--

        for i:=x1;i>=x0;i--{
            result = append(result, matrix[i][y0])
        }
        y0++;
    }

    return result
}

题解

方法一:

1

func spiralOrder(matrix [][]int) []int {
    if len(matrix) == 0 || len(matrix[0]) == 0 {
        return []int{}
    }
    rows, columns := len(matrix), len(matrix[0])
    visited := make([][]bool, rows)
    for i := 0; i < rows; i++ {
        visited[i] = make([]bool, columns)
    }

    var (
        total = rows * columns
        order = make([]int, total)
        row, column = 0, 0
        directions = [][]int{[]int{0, 1}, []int{1, 0}, []int{0, -1}, []int{-1, 0}}
        directionIndex = 0
    )

    for i := 0; i < total; i++ {
        order[i] = matrix[row][column]
        visited[row][column] = true
        nextRow, nextColumn := row + directions[directionIndex][0], column + directions[directionIndex][1]
        if nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn] {
            directionIndex = (directionIndex + 1) % 4
        }
        row += directions[directionIndex][0]
        column += directions[directionIndex][1]
    }
    return order
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/spiral-matrix/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

时间:O(mn)
空间:O(mn)

方法二:

2

func spiralOrder(matrix [][]int) []int {
    if len(matrix) == 0 || len(matrix[0]) == 0 {
        return []int{}
    }
    var (
        rows, columns = len(matrix), len(matrix[0])
        order = make([]int, rows * columns)
        index = 0
        left, right, top, bottom = 0, columns - 1, 0, rows - 1
    )

    for left <= right && top <= bottom {
        for column := left; column <= right; column++ {
            order[index] = matrix[top][column]
            index++
        }
        for row := top + 1; row <= bottom; row++ {
            order[index] = matrix[row][right]
            index++
        }
        if left < right && top < bottom {
            for column := right - 1; column > left; column-- {
                order[index] = matrix[bottom][column]
                index++
            }
            for row := bottom; row > top; row-- {
                order[index] = matrix[row][left]
                index++
            }
        }
        left++
        right--
        top++
        bottom--
    }
    return order
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/spiral-matrix/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

时间:O(mn)
空间:O(1)

11111

长进

  1. 矩阵层数有奇数偶数,情况不同,第一次未做考虑,样例2过不去。

你可能感兴趣的:(LeetCode刷题,矩阵,线性代数,数据结构,golang)