leetcode面试题之go实现顺时针打印矩阵

顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
示例 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]

Go语言实现

package main
func printMatrix(matrix [][]int) []int {
	result := []int{}
	row := len(matrix)
	if row == 0 {
		return result
	}
	column := len(matrix[0])
	if column == 0 {
		return result
	}
	//定义下标变量
	top := 0
	bottom := row - 1
	left := 0
	right := column - 1
	for left <= right && top <= bottom {
		//从左往右,行数为top,列数从left开始,+1直到right
		for i := left; i <= right; i++ {
			result = append(result, matrix[top][i])
		}
		//从上往下,行数从top+1开始,+1直到bottom,列数为right
		for i := top + 1; i <= bottom; i++ {
			result = append(result, matrix[i][right])
		}
		//row不重复,从右往左,行数为bottom,列数从right-1开始,-1直到left
		if top != bottom {
			for i := right - 1; i >= left; i-- {
				result = append(result, matrix[bottom][i])
			}
		}
		//column不重复,从下往上,行数从bottom-1开始,-1直到top-1,所以没有等号
		if left != right {
			for i := bottom - 1; i > top; i-- {
				result = append(result, matrix[i][left])
			}
		}
		//一圈结束
		left++
		right--
		top++
		bottom--
	}
	return result
}
func main(){
a:=[][]int{
	{1, 2, 3},
	{4, 5, 6},
	{7, 8, 9},
}
fmt.Println(printMatrix(a))
}

运行结果在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43318506/article/details/106570440
今日推荐