From 2236bdab89d96a35ab25671c556e94775c1eacca Mon Sep 17 00:00:00 2001 From: Cool <747682928@qq.com> Date: Sat, 7 Sep 2024 23:57:45 +0800 Subject: [PATCH] 2024/9/07 LeetCode Hot100 matrix --- .../java/com/cool/hot100/matrix/Num54.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/com/cool/hot100/matrix/Num54.java diff --git a/src/main/java/com/cool/hot100/matrix/Num54.java b/src/main/java/com/cool/hot100/matrix/Num54.java new file mode 100644 index 0000000..2682ae0 --- /dev/null +++ b/src/main/java/com/cool/hot100/matrix/Num54.java @@ -0,0 +1,51 @@ +package com.cool.hot100.matrix; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * + * @Author: Cool + * @Date: 2024/09/07/23:00 + * DayNumber 1 + * Hard 2 + * Level 4 + */ +public class Num54 { + + public List spiralOrder(int[][] matrix) { + List list=new ArrayList<>(); + if(matrix.length==0){ + return list; + } + int up=0; //上界 + int down=matrix.length-1; //下界 + int left=0; + int right=matrix[0].length-1; + while(true){ + // 向右遍历 + for(int i=left;i<=right;i++){ + list.add(matrix[up][i]); + } + if(++up>down)break; + // 向下遍历 + for(int i=up;i<=down;i++){ + list.add(matrix[i][right]); + } + if(--right=left;i--){ + list.add(matrix[down][i]); + } + if(--down=up;i--){ + list.add(matrix[i][left]); + } + if(++left>right) break; + } + return list; + } + +}