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 ...
随机推荐
- Alpha,Beta,RC,RTM,EVAL,CTP,OEM,RTL,VOL
微软的一个系统(如Win 7)或开发工具(VS系列),往往会对应很多种版本,下面就介绍一下这些版本的含义: Alpha (阿尔法,希腊字母的第一位'α',代表最初的版本) Alpha是内部测试版, ...
- win7右下角的同步中心怎么去除
“开始”-“运行”-“regedit”,在“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Syncmgr\Handlers” ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- Mininet 系列实验(五)
实验内容 实现一个单个交换机的拓扑,添加一个交换机,和N个主机到网络中.交换机和主机之间的每个链路能够设置带宽.延迟时间.以及丢包率.创建一个包含一个交换机和四个主机的网络,使用iperf测试主机之间 ...
- linux内核设计与实现一书阅读整理 之第三章
chapter 3 进程管理 3.1 进程 进程就是处于执行期的程序. 进程就是正在执行的程序代码的实时结果. 内核调度的对象是线程而并非进程. 在现代操作系统中,进程提供两种虚拟机制: 虚拟处理器 ...
- AS可视化布局中文乱码
求助android studio 的可视化布局中文乱码-CSDN论坛-CSDN.NET-中国最大的IT技术社区http://bbs.csdn.net/topics/391887442 Android ...
- Chapter11(关联容器)--C++Prime笔记
1.关联容器: map关键字-值对,经常被称为关联数组 set中每个元素只有一个关键字,即只保存关键字的容器 ①允许重复的关键字的容器名字都包含multi. ②不保持关键字顺序存储的容器的名字都以但粗 ...
- Docker Swarm高可用性
一.前言 在Docker Swarm集群中,Swarm manager负责管理整个集群,如果管理节点manager出现故障,虽然不会影响现有的服务和工作节点,但是我们不能继续管理我们的docker s ...
- JS中的异步与回调
问题的引出:在js中使用异步调用时,有可能会出现在异步的回调函数中设置调用之外的变量值,但在异步调用完成后去使用变量,却发现这些变量值并没有被成功设置的情况.如: google map中的地理编码,地 ...
- 高质量API网关组件实现
PI网关组件的作用? 1.网关直接代替MVC当中的Controller层,减少编码量提高开发效率 2.统一API接口的出入参格式,提高API的友好性 3.自动检测API接口规范,提高接口的质量 4.统 ...