Ai
1 Star 0 Fork 0

wu-xjg/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Code54.java 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
wu-xjg 提交于 2025-02-18 22:18 +08:00 . day02
package Code;
import java.util.ArrayList;
import java.util.List;
public class Code54 {
static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix == null || matrix[0].length == 0){
return res;
}
int m = matrix.length;
int n= matrix[0].length;
int top = 0, bottom = m-1, left = 0, right = n-1;
while(top <= bottom && left <= right){
for(int j = left; j <= right; j++){
res.add(matrix[top][j]);
}
top++;
for(int i = top; i<= bottom; i++){
res.add(matrix[i][right]);
}
right--;
if(top <= bottom) {
for (int j = right; j >= left; j--) {
res.add(matrix[bottom][j]);
}
bottom--;
}
if(left <= right){
for(int i = bottom; i>=top; i--){
res.add(matrix[i][left]);
}
left++;
}
}
return res;
}
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
List<Integer> result = spiralOrder(matrix);
System.out.println(result);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wu-xjg/leet-code.git
git@gitee.com:wu-xjg/leet-code.git
wu-xjg
leet-code
LeetCode
master

搜索帮助