持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情
Leetcode : leetcode-cn.com/problems/sh…
GitHub : github.com/nateshao/le…
剑指 Offer 29. 顺时针打印矩阵
题目描述 :输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
示例 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]限制:
0 <= matrix.length <= 1000 <= matrix[i].length <= 100
Go
package main
func main() {
}
func spiralOrder(matrix [][]int) []int {
if len(matrix) == 0 { // 长度为0,返回nil
return nil
}
res := []int{}
top, bottom, left, right := 0, len(matrix)-1, 0, len(matrix[0])-1 //初始各个边界
for bottom >= top && right >= left { //循环条件,下大于等于上,右大于等于左
for i := left; i <= right; i++ { //top行的从左到右遍历
res = append(res, matrix[top][i])
}
top++ //top行遍历完了,top往下移动
for i := top; i <= bottom; i++ { //right列的从上到下遍历
res = append(res, matrix[i][right])
}
right-- //right列遍历完了,right向左移动
if left > right || top > bottom { //这里一定要做一个判断,因为如果只剩下一个元素就会出错
break
}
for i := right; i >= left; i-- { //bottom行的从右往左遍历
res = append(res, matrix[bottom][i])
}
bottom-- //bottom往上移动
for i := bottom; i >= top; i-- { //left列的从下到上遍历
res = append(res, matrix[i][left])
}
left++ //left向右移动
}
return res
}
解题思路:
根据题目示例
matrix = [[1,2,3],[4,5,6],[7,8,9]]的对应输出[1,2,3,6,9,8,7,4,5]可以发现,顺时针打印矩阵的顺序是 “从左向右、从上向下、从右向左、从下向上” 循环。
- 因此,考虑设定矩阵的“左、上、右、下”四个边界,模拟以上矩阵遍历顺序。
算法流程:
-
空值处理:当matrix 为空时,直接返回空列表 [] 即可。
-
初始化:矩阵左、右、上、下四个边界1,r,t,b,于打印的结果列表res。
-
循环打印:“从左向右、 从上向下、从右向左、从下向.上”四个方向循环,每个方向打印中做以下三件事(各方向的具体信息见下表) ; 1.根据边界打印,即将元素按顺序添加至列表res 尾部; 2.边界向内收缩1 (代表已被打印) ; 3.判断是否打印完毕(边界是否相遇),若打印完毕则跳出。
-
返回值: 返回res即可。
| 打印方向 | 1. 根据边界打印 | 2. 边界向内收缩 | 3. 是否打印完毕 |
|---|---|---|---|
| 从左向右 | 左边界l ,右边界 r | 上边界 t 加 1 | 是否 t > b |
| 从上向下 | 上边界 t ,下边界b | 右边界 r 减 1 | 是否 l > r |
| 从右向左 | 右边界 r ,左边界l | 下边界 b 减 1 | 是否 t > b |
| 从下向上 | 下边界 b ,上边界t | 左边界 l 加 1 | 是否 l > r |
复杂度分析:
-
时间复杂度 O(MN): M, N分别为矩阵行数和列数。
-
空间复杂度 O(1): 四个边界
l,r,t,b使用常数大小的 额外 空间(res为必须使用的空间)。
代码:
Java 代码利用了
++操作的便利性
res[x++]等价于先给res[x]赋值,再给x自增 11 ;++t > b等价于先给t自增 11 ,再判断t > b逻辑表达式。
java
package com.nateshao.sword_offer.topic_22_spiralOrder;
/**
* @date Created by 邵桐杰 on 2021/11/25 9:59
* @微信公众号 千羽的编程时光
* @个人网站 www.nateshao.cn
* @博客 https://nateshao.gitee.io
* @GitHub https://github.com/nateshao
* @Gitee https://gitee.com/nateshao
* Description: 剑指 Offer 22. 顺时针打印矩阵
* https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof
*/
public class Solution {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
printMatrix(matrix);
spiralOrder(matrix);
System.out.println("=========");
printMatrix(matrix);
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i != matrix.length; i++) {
for (int j = 0; j != matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static int[] spiralOrder(int[][] matrix) {
if (matrix.length == 0) return new int[0];
int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
int[] res = new int[(r + 1) * (b + 1)];
while (true) {
//从左往右
//列在变,列为循环值
//从左往右的下一步是往下走,上边界内缩,故++t
for (int i = l; i <= r; i++) res[x++] = matrix[t][i];
if (++t > b) break;
//从上往下,行在变
//从上往下的下一步是从右往左,右边界收缩,--r
for (int i = t; i <= b; i++) res[x++] = matrix[i][r];
if (l > --r) break;
//从右向左,列在变
//从右往左的下一步是从下往上,下边界收缩,--b
for (int i = r; i >= l; i--) res[x++] = matrix[b][i];
if (t > --b) break;
//从下到上,行在变
//从下到上的下一步是从左到右,左边界收缩,++l
for (int i = b; i >= t; i--) res[x++] = matrix[i][l];
if (++l > r) break;
}
return res;
}
}