当前位置: 首页 > news >正文

【Day 13】189.轮转数组

文章目录

  • 189.轮转数组
  • 题目:
  • 思路:
  • 代码实现(Go):

189.轮转数组

题目:

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


思路:

上下左右转圈圈:
从左到右,顶部一层遍历完往下移一位,top++
从上到下,遍历完右侧往左移一位,right--
从右到左,判断top <= bottom,遍历完底部上移,bottom--
从下到上,判断left <= right,遍历完左侧右移,left++

后两步需要判断的原因:
在循环内部,上边界和右边界先被使用并收缩(top++、right--)
当到达下边界或左边界时,边界可能已经交错了
top > bottom 或 left > right
需要判断才能安全遍历


代码实现(Go):

详细注解:

// package main// import "fmt"func spiralOrder(matrix [][]int) []int {// 矩阵为空或矩阵行为空(行存在,但列为空,也就是空行),1 <= m,n <= 10,所以可不写if len(matrix) == 0 || len(matrix[0]) == 0 {return []int{}}// 获取矩阵行数和列数m, n := len(matrix), len(matrix[0])// 定义四个边界top, bottom := 0, m-1 // 上下边界left, right := 0, n-1 // 左右边界res := []int{} // 用于存储结果// 当边界没有交错时,继续循环for top <= bottom && left <= right {// 1)上边界:从左到右遍历 top 行for j := left; j <= right; j++ {res = append(res, matrix[top][j])}top++ // 上边界收缩,下一圈不再遍历这行// 2)右边界:从上到下遍历 right 列for i := top; i <= bottom; i++ {res = append(res, matrix[i][right])}right-- // 右边界收缩// 3)下边界:从右到左遍历 bottom 行// 注意需要判断 top <= bottom,防止重复遍历单行if top <= bottom { // 只有当 top <= bottom 时,才遍历底部一行,并且收缩下边界for j := right; j >= left; j-- {res = append(res, matrix[bottom][j])}bottom-- // 下边界收缩}// 4)左边界:从下到上遍历 left 列// 注意需要判断 left <= right,防止重复遍历单列if left <= right { // 只有当 left <= right 时,才遍历左边一列,并且收缩左边界for i := bottom; i >= top; i-- {res = append(res, matrix[i][left])}left++ // 左边界收缩}}return res
}// func main() {
// 	matrix := [][]int{
// 		{1, 2, 3},
// 		{4, 5, 6},
// 		{7, 8, 9},
// 	}
// 	fmt.Println(spiralOrder(matrix)) // 输出: [1 2 3 6 9 8 7 4 5]
// }

无注释:

// package main// import "fmt"func spiralOrder(matrix [][]int) []int {if len(matrix) == 0 || len(matrix[0]) == 0 {return []int{}}m, n := len(matrix), len(matrix[0])top, bottom := 0, m-1left, right := 0, n-1res := []int{}for top <= bottom && left <= right {for j := left; j <= right; j++ {res = append(res, matrix[top][j])}top++for i := top; i <= bottom; i++ {res = append(res, matrix[i][right])}right--if top <= bottom {for j := right; j >= left; j-- {res = append(res, matrix[bottom][j])}bottom--}if left <= right {for i := bottom; i >= top; i-- {res = append(res, matrix[i][left])}left++}}return res
}// func main() {
// 	matrix := [][]int{
// 		{1, 2, 3},
// 		{4, 5, 6},
// 		{7, 8, 9},
// 	}
// 	fmt.Println(spiralOrder(matrix)) // 输出: [1 2 3 6 9 8 7 4 5]
// }

http://www.dtcms.com/a/350079.html

相关文章:

  • 项目文章|MeRIP-seq助力解析m6A RNA甲基化与康乃馨花衰老的调控机制
  • Day8--HOT100--160. 相交链表,206. 反转链表,234. 回文链表,876. 链表的中间结点
  • 30.throw抛异常
  • 项目前后端分离部署
  • LVM基本操作
  • LeetCode100-189轮转数组
  • 20.15 Hugging Face Whisper-large-v2中文微调实战:LoRA+混合精度单卡训练指南,3倍效率省90%显存
  • 正则表达式学习(基础)
  • AUTOSAR进阶图解==>AUTOSAR_RS_Features
  • 电脑隐私安全防护|快速清理Windows系统/浏览器/应用数据,支持文件粉碎与磁盘级擦除!
  • 从MyJUnit反思Java项目的工程实践(版本控制篇)
  • 数据库迁移幂等性介绍(Idempotence)(Flyway、Liquibase)ALTER、ON DUPLICATE
  • RabbitMQ面试精讲 Day 30:RabbitMQ面试真题解析与答题技巧
  • 深入解析MyBatis Mapper接口工作原理
  • Ubuntu24.04配置yolov5
  • 封装的form表单,校验规则(rules)只在提交时触发,为空时点击提交触发,再次输入内容也不显示校验规则(rules)
  • 机器学习】(12) --随机森林
  • Day27 进程管理(PCB、状态、调度、原语与资源管理)
  • pikachu之Over permission
  • 基于SpringBoot的宠物领养系统的设计与实现(代码+数据库+LW)
  • QML中的Connections
  • Vue 3 defineOptions 完全指南:让组件选项声明更现代化
  • vb6编绎COM DLL(ACTIVEX对象)时兼容性设置
  • bisheng 后端初始化数据(main.py > init_data.py)
  • 25072班8.25日 数据结构作业
  • 04-Maven工具介绍
  • kafka 副本集设置和理解
  • 《Spring Boot 进阶:从零到一打造自定义 @Transactional》 ——支持多数据源、动态传播行为、可插拔回滚策略
  • AI系列 - Claude 与 Qwen 模型自动补全对比:谁更胜一筹?
  • 电力系统稳定性的挑战与智能控制新范式