Spiral Matrix I

Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.

Example

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.

Example

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. 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 ...

  5. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  6. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  7. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  8. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  9. 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 ...

随机推荐

  1. Convolutional Neural Networks卷积神经网络(二)

    转自http://blog.csdn.net/zouxy09/article/details/8781543 CNNs是第一个真正成功训练多层网络结构的学习算法.它利用空间关系减少需要学习的参数数目以 ...

  2. hdu6447 YJJ's Salesman

    这个题意和数据范围一看就是离散化之后树状数组优化DP.给的"从左下方走上去才能拿到收益"的性质其实可以当成"必须从横纵坐标严格比某个点小的地方转移过来".1A了 ...

  3. final,finally和 finalize的区别

    中等区别: 虽然这三个单词在Java中都存在,但是并没有太多关联:  final:java中的关键字,修饰符. 1.如果一个类被声明为final,就意味着它不能再派生出新的子类,不能作为父类被继承.因 ...

  4. UltraISO制作U盘启动盘

    第一步:导入镜像文件 文件->打开 第二步: 启动->写入硬盘映像 隐藏启动分区选择“无” 然后等待即可.

  5. 遇到问题----java----myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件

    myeclipse或者eclipse发布的项目时配置文件不更新或者无配置文件. 正常的web项目有目录 src/main/resources 和 src/main/java 这两个目录默认在编译发布时 ...

  6. 封装个StringBuffer,用array join的方式拼接字符串

    (function(window) { var core_ArrPro = Array.prototype; var core_slice = core_ArrPro.slice; var core_ ...

  7. ORB算法介绍(转)

    本文为原创文章,转载请注明出处:http://blog.csdn.net/yang843061497/article/details/38553765 绪论 假如我有2张美女图片,我想确认这2张图片中 ...

  8. sql 事务的四种隔离级别

    在 SQL 标准中定义了四种隔离级别,每一种级别都规定了一个事务中所做的修改,哪些在事务内和事务间是可见的,哪些是不可见的.较低级别的隔离通常可以执行更高的并发,系统的开销也更低. read unco ...

  9. Linux运维三:系统目录结构

    Linux系统目录结构官方参考:http://www.pathname.com/fhs/ 1:Linux树状目录结构图 下面目录中标红的是必须要掌握的! 2:根目录  目录 描述 / 第一层次结构的根 ...

  10. (转)tomcat+nginx+redis实现均衡负载、session共享(一)

    在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新.但这可能会出现一些状况: 1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一) ...