Leetcode53_Spiral_Matrix
Spiral_Matrix
https://leetcode-cn.com/problems/spiral-matrix/
//当行数只有一行:
1、 n = 1;
m -> 0; //当列数只有一列
2、m = 1;
n -> 0; //行数为 n, 列数为m
3、 。。。。。。
。。。。。。
。。。。。。
。。。。。。
1) 第一次
x, y
y = 0; x : 0 -> m-1;
x = m-1; y : 0 -> n-1;
y = n-1; x : m -> 0;
x = 0; y : n -> 1 2) 第二次
y = 1; x : 0 -> m -2;
x = m-2; y : 1 -> n - 2;
y = n -2; x:m -1 -> 1;
x = m-2; y : n-1 -> 2 ...以此类推
代码
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (matrix == null || matrix.length == 0) {
return null;
}
int n = matrix[0].length;
int m = matrix.length;
int x = 0, y = 0;
int i, j;
while (n > 0 && m > 0) {
if (n == 1) {
for (int k = 0; k < m; k++) {
list.add(matrix[k][0]);
}
return list;
}
if (m == 1) {
for (int k = 0; k < n; k++) {
list.add(matrix[0][k]);
}
return list;
}
i = x;
j = y;
for (; i < n; i++) {
list.add(matrix[j][i]);
}
i --;
j ++;
for (; j < m; j++) {
list.add(matrix[j][i]);
}
i --;
j --;
for(; i >= x ; i--) {
list.add(matrix[j][i]);
}
j--;
i++;
for(; j >= y + 1; j--) {
list.add(matrix[j][i]);
}
x++;
y++;
m--;
n--;
}
return list;
}
}
Leetcode53_Spiral_Matrix的更多相关文章
随机推荐
- go语言设计模式之interpreter
interpreter.go package interpreter import ( //"fmt" "strconv" "strings" ...
- MongoDB学习笔记(一、MongoDB入门)
目录: 为什么要使用nosql mongo的简介 应用场景 入门demo 为什么要使用nosql: 随着互联网的发展,用户数量激增,访问量的上涨,传统的关系型数据库的性能也趋于瓶颈. 关系型数据库难以 ...
- 04. Go 语言流程控制
Go 语言流程控制 流程控制是每种编程语言控制逻辑走向和执行次序的重要部分,流程控制可以说是一门语言的"经脉". Go 语言的常用流程控制有 if 和 for,而 switch 和 ...
- BoW算法及DBoW2库简介(二)
一.BoW算法 用OpenCV实现了最简单的BoW算法进行了一次小规模的图像检索任务,使用UKbench数据库,算法原理和网上的描述差不多,使用K-means算法进行聚类,这里使用KDTree算法进行 ...
- RMAN详细教程(四):备份脚本实战操作
RMAN详细教程(一):基本命令代码 RMAN详细教程(二):备份.检查.维护.恢复 RMAN详细教程(三):备份脚本的组件和注释 RMAN详细教程(四):备份脚本实战操作 1.为了安全起见,先将数据 ...
- linux 主机通过虚拟机(win10)上网
公司内网必须安装安全软件(exe)才可以上网,但是我的系统是deepin,用deepin-wine无法安软该exe,于是用vmware安装了win10虚拟机,通过虚拟机上网 先简单介绍下vmware以 ...
- IT兄弟连 HTML5教程 HTML5行业的发展预测
现在的互联网市场上,HTML5在快速地成长,甚至是未来几年里将会有很多公司进入HTML5这个领域,HTML5也会像传统的Flex,Flash,Silverlight和Objective-C那样,更容易 ...
- 【MySQL报错】ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot exec ...
- error while loading shared libraries
https://stackoverflow.com/questions/480764/linux-error-while-loading-shared-libraries-cannot-open-sh ...
- 解决ie下vue列表数据不能即时刷新的问题
项目上要兼容IE浏览器(客户要求),发现之前在谷歌浏览器下,操作(增删改查)列表后列表能即时刷新(双向绑定),IE下却不行. 自己调试一下发现,在IE11下,如果GET请求请求相同的URL,默认会使用 ...