Spiral Matrix I & II
Spiral Matrix I
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
分析:
从上,右,下,左打印。
public class Solution {
public int[][] generateMatrix(int n) {
int[][] arr = new int[n][n];
int a = ;
int b = n - ;
int k = ;
while (a < b) {
for (int i = a; i <= b; i++) {
arr[a][i] = k++;
}
for (int i = a + ; i <= b - ; i++) {
arr[i][b] = k++;
}
for (int i = b ; i >= a; i--) {
arr[b][i] = k++;
}
for (int i = b - ; i >= a + ; i--) {
arr[i][a] = k++;
}
a++;
b--;
}
// if n is odd, it will be executed, if it is even, it won't be executed.
if (a == b) {
arr[a][b] = k;
}
return arr;
}
}
Spiral Matrix II
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
分析:
拿到左上角和右下角的坐标,然后从上,右,下,左打印。然后更新坐标。
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
if (matrix == null || matrix.length == || matrix[].length == ) return list;
int a = , b = ;
int x = matrix.length - , y = matrix[].length - ;
while (a <= x && b <= y) {
// top row
for (int i = b; i <= y; i++) {
list.add(matrix[a][i]);
}
// right column
for (int i = a + ; i <= x - ; i++) {
list.add(matrix[i][y]);
}
// bottom row
if (a != x) {
for (int i = y; i >= b; i--) {
list.add(matrix[x][i]);
}
}
// left column
if (b != y) {
for (int i = x - ; i >= a + ; i--) {
list.add(matrix[i][b]);
}
}
a++;
b++;
x--;
y--;
}
return list;
}
}
Spiral Matrix I & II的更多相关文章
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Spiral Matrix I&&II
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
随机推荐
- struts 跳转的四种常用类型
1 dispatcher 默认的跳转类型 地址栏不变 2.redirect 跳转后地址会变化 3 chain 跳转到一个动作类 地址栏不会变 4 redirectAction 跳转到一个动作类 地址栏 ...
- hibernate关联关系
hibernate是一个强大的ORM框架,为了使用面向对象的方式管理数据库,hibernate提供了4中关系设置: 1.一对一 (one-to-one) 2.一对多 (one-to-many) 3.多 ...
- springmvc+mybatis 处理图片(一):上传图片
一直觉得上传图片文件之类的很难,所以最后才处理图片,发现也并没有那么难,开始正文. 思路:将前台上传的file存到MutipartFile类型字段中,再将MulipartFile转换为pojo类中的b ...
- Spring面试,IoC和AOP的理解, @Transactional原理及使用
spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...
- 怎么解决Xing欲
怎么解决Xing欲 来源:微信号 王路在隐身 这是知乎上的一道问题.原题叫<和尚怎么解决性欲>. 本来由出家人回答更合适,但估计出家人一般不太愿意回答. 我看了几十个答案,几乎都是在调侃出 ...
- 学习Spring Boot:(六) 集成Swagger2
前言 Swagger是用来描述和文档化RESTful API的一个项目.Swagger Spec是一套规范,定义了该如何去描述一个RESTful API.类似的项目还有RAML.API Bluepri ...
- BZOJ 3224 普通平衡树 | 平衡树模板
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- 安装logstash5.4.1,并使用grok表达式收集nginx日志
关于收集日志的方式,最简单性能最好的应该是修改nginx的日志存储格式为json,然后直接采集就可以了. 但是实际上会有一个问题,就是如果你之前有很多旧的日志需要全部导入elk上查看,这时就有两个问题 ...
- 配置nginx为FastDFS的storage server提供http访问接口
1.拉取模块代码 # git clone https://github.com/happyfish100/fastdfs-nginx-module.git 2.编译安装nginx,添加支持fastdf ...