0054leetcode算法实现之螺旋矩阵spiralMatrixpython&golang实现 Marathon

给你一个 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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/spiral-matrix

python

# 螺旋矩阵
class Solution:
    def spiralMatrix(self, matrix):
        """
        模拟行为,上下左右四个边界,每次边界变化,即上++右--下--左++,当top>bottom或者left>right时,break
        -向右打印,top行号不变,left->right,走完top++
        -向下打印,right列号不变,top->bottom,走完right--
        -向左打印,bottom行号不变,right->left,走完bottom++
        -向上打印,left列号不变,bottom->top,走完left++
        :param matrix: [][]int
        :return:[]int
        """
        res = []
        if len(matrix) == 0: # 空矩阵直接返回[]
            return res
        top,bottom,left,right = 0,len(matrix)-1,0,len(matrix[0])-1 # 定义四个变量,分别为上下左右边界
        while top <= bottom and left <= right: # 仅当上边界小于等于下边界,左边界小于等于右边界时,循环打印
            for i in range(left, right+1): # 向右打印,此时行号固定,变化的是列号,即top固定,此方向left为最开始打印,right为最后打印,每次加入res尾部
                res.append(matrix[top][i])
            top += 1 # 向右走完此轮,top--,下移一个单位
            for i in range(top, bottom+1): # 向下打印,此时列号固定,由于是向下即right固定,变化的是上至下的行号,此方向top为最开始打印,bottom为最后打印,每次加入res尾部
                res.append(matrix[i][right])
            right -= 1 # 向下走完此轮,right--,左移一个单位
            if top > bottom or left > right: # 检查top与bottom或left与right有无越界
                break
            for i in range(right, left-1, -1): # 向左打印,此时行号固定,bottom固定,变化的右至左的列号,每次打印加入res尾部
                res.append(matrix[bottom][i])
            bottom -= 1 # 向左走完此轮,bottom++
            for i in range(bottom, top-1, -1): # 向上打印,此时列号固定,即left固定,变化的是下至上的行号,每次打印加入res的尾部
                res.append(matrix[i][left])
            left += 1 # 向上走完此轮,left++
        return res

if __name__ == "__main__":
    matrix = [
        [1,2,3,4,5],
        [6,7,8,9,10],
        [11,12,13,14,15],
        [16,17,18,19,20]
    ]
    test = Solution()
    print(test.spiralMatrix(matrix))

golang

//+build ignore

package main

import "fmt"

func main() {
	matrix := [][]int{
		{1, 2, 3, 4, 5},
		{6, 7, 8, 9, 10},
		{11, 12, 13, 14, 15},
		{16, 17, 18, 19, 20},
	}
	matrix2 := [][]int{
		{1, 2, 3}, {4, 5, 6}, {7, 8, 9},
	}
	fmt.Println(spiralMatrix(matrix))
	fmt.Println(spiralMatrix(matrix2))
}

func spiralMatrix(matrix [][]int) []int {
	res := []int{}
	if len(matrix) == 0 {
		return res
	}
	top, bottom, left, right := 0, len(matrix)-1, 0, len(matrix[0])-1

	for top <= bottom && left <= right {
		for i := left; i <= right; i++ {
			res = append(res, matrix[top][i])
		}
		top++
		for i := top; i <= bottom; i++ {
			res = append(res, matrix[i][right])
		}
		right--
		if top > bottom || left > right {
			break
		}
		for i := right; i >= left; i-- {
			res = append(res, matrix[bottom][i])
		}
		bottom--
		for i := bottom; i >= top; i-- {
			res = append(res, matrix[i][left])
		}
		left++
	}
	return res
}

原文地址:https://www.cnblogs.com/davis12/p/15477963.html