Golang | Leetcode Golang题解之第59题螺旋矩阵II

题目:

题解:

Go 复制代码
func generateMatrix(n int) [][]int {
    matrix := make([][]int, n)
    for i := range matrix {
        matrix[i] = make([]int, n)
    }
    num := 1
    left, right, top, bottom := 0, n-1, 0, n-1
    for left <= right && top <= bottom {
        for column := left; column <= right; column++ {
            matrix[top][column] = num
            num++
        }
        for row := top + 1; row <= bottom; row++ {
            matrix[row][right] = num
            num++
        }
        if left < right && top < bottom {
            for column := right - 1; column > left; column-- {
                matrix[bottom][column] = num
                num++
            }
            for row := bottom; row > top; row-- {
                matrix[row][left] = num
                num++
            }
        }
        left++
        right--
        top++
        bottom--
    }
    return matrix
}
相关推荐
北冥you鱼1 小时前
Go 语言初始化 MySQL 数据库实战指南:从零到生产环境部署
数据库·mysql·golang
Tisfy2 小时前
LeetCode 1288.删除被覆盖区间:排序(非二重循环暴力)
算法·leetcode·题解·排序
知无不研2 小时前
算法:Fizz Buzz解题思路
c++·算法·leetcode
玛卡巴卡ldf5 小时前
【LeetCode 手撕算法】(细节知识点总结)
java·数据结构·算法·leetcode·力扣
名字还没想好☜6 小时前
Go error 处理:errors.Is/As 与错误包装
开发语言·后端·golang·go·错误处理
tkevinjd7 小时前
力扣322-零钱兑换
算法·leetcode·动态规划
Frostnova丶8 小时前
(13)LeetCode 53. 最大子数组和
算法·leetcode·职场和发展
什巳8 小时前
JAVA练习277- 找到字符串中所有字母异位
java·开发语言·算法·leetcode
灯澜忆梦8 小时前
Go 工程管理与作用域
golang
Sw1zzle16 小时前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode