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
}
相关推荐
踩坑记录1 小时前
leetcode hot100 23. 合并 K 个升序链表 hard 分治 迭代
leetcode·链表
期末考复习中,蓝桥杯都没时间学了1 小时前
力扣刷题13
数据结构·算法·leetcode
会飞的战斗鸡2 小时前
JS中的链表(含leetcode例题)
javascript·leetcode·链表
多米Domi0112 小时前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
渐暖°2 小时前
【leetcode算法从入门到精通】5. 最长回文子串
vscode·算法·leetcode
今天_也很困2 小时前
LeetCode热题100-560. 和为 K 的子数组
java·算法·leetcode
v_for_van2 小时前
力扣刷题记录2(无算法背景,纯C语言)
c语言·算法·leetcode
alphaTao3 小时前
LeetCode 每日一题 2026/1/26-2026/2/1
算法·leetcode
We་ct3 小时前
LeetCode 54. 螺旋矩阵:两种解法吃透顺时针遍历逻辑
前端·算法·leetcode·矩阵·typescript
We་ct5 小时前
LeetCode 36. 有效的数独:Set实现哈希表最优解
前端·算法·leetcode·typescript·散列表