import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/spiral-matrix/
*
* Created by lverpeng on 2017/7/19.
*
* Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
*
* For 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 SpiralMatrix { /**
* 主要是边界问题
*
* @param matrix
* @return
*/
public int[] join (int[][] matrix) {
if (matrix.length <= 0) {
return new int[]{};
}
int m = matrix.length;
int n = matrix[0].length;
int[] result = new int[m * n];
int row = 0;
int col = 0;
int index = 0;
for (; row < (m + 1) / 2 && col < (n + 1) / 2; row ++, col ++) {
for (int i = col; i < n - col; i++) {
result[index++] = matrix[row][i];
}
for (int i = row + 1; i < m - row; i++) {
result[index++] = matrix[i][n - col - 1];
}
for (int i = n - col - 2; m - row - 1 > row && i >= col; i--) {
result[index++] = matrix[m - row - 1][i];
}
for (int i = m - row - 2; n - col - 1 > col && i > row; i--) {
result[index++] = matrix[i][col];
}
} return result;
} public static void main(String[] args) {
SpiralMatrix spiralMatrix = new SpiralMatrix();
int[][] matrix = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] matrix1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
int[][] matrix2 = new int[][]{
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix3 = new int[][]{
{1, 2, 3}
};
int[][] matrix4 = new int[][]{
{1},
{4},
{5}
}; System.out.println(Arrays.toString(spiralMatrix.join(matrix)));
System.out.println(Arrays.toString(spiralMatrix.join(matrix1)));
System.out.println(Arrays.toString(spiralMatrix.join(matrix2)));
System.out.println(Arrays.toString(spiralMatrix.join(matrix3)));
System.out.println(Arrays.toString(spiralMatrix.join(matrix4)));
}
}

leetcode — spiral-matrix的更多相关文章

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

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

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  3. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

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

  5. [LeetCode]Spiral Matrix 54

    54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...

  6. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  7. [Leetcode] spiral matrix ii 螺旋矩阵

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

  8. LeetCode——Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  10. [leetcode]Spiral Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. 第四周助教工作总结——NWNU李泓毅

    1.    助教博客链接: https://www.cnblogs.com/NWNU-LHY/ 2.    作业要求链接: www.cnblogs.com/nwnu-daizh/p/10487329. ...

  2. web安全系列1:入侵的途径

    大家好,接下来的很长一段时间我都会介绍和web安全有关的知识,欢迎大家关注和转发. 话不多说,我们首先来看看今天的主题----入侵的途径.当然,今天介绍的都是针对web网站的常用手法和技巧. 不可否认 ...

  3. django中的modelform和modelfoemset

    一. ModelForm ModelForm是根据Model来定制的Form 二. ModelForm的创建 from django import forms from app import mode ...

  4. hadoop HDFS常用文件操作命令

    命令基本格式: hadoop fs -cmd < args > 1. ls 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls /dir hadoop fs -ls -R ...

  5. REdis CPU百分百问题分析

    REdis版本:4.0.9 运行环境:Linux 3.10.107 x86_64 gcc_version:4.8.5 结论:是一个BUG,在4.0.11版本中被作者antirez所修复 现象: 1)  ...

  6. <笔记>TP5的分页传递额外参数

    默认生成的分页只有page一个参数,若需要提供额外的参数才能访问分页(例如查询结果的分页,需要传入查询关键字的参数才能显示结果),则需要设置额外参数query

  7. Linux 区别 chown和chmod的用法

    chown用法用来更改某个目录或文件的用户名和用户组的chown 用户名:组名 文件路径(可以是就对路径也可以是相对路径)例1:chown root:root /tmp/tmp1就是把tmp下的tmp ...

  8. QEMU KVM Libvirt手册(5) – snapshots

    前面讲了QEMU的qcow2格式的internal snapshot和external snapshot,这都是虚拟机文件格式的功能. 这是文件级别的. 还可以是文件系统级别的,比如很多文件系统支持s ...

  9. 王垠的40行代码,究竟diao在哪里

    王垠是谁? 不用我说了吧!!! 别傻谈,亮码瞧! ;; A simple CPS transformer which does proper tail-call and does not;; dupl ...

  10. JAVA编程思想的理解

    1)POP--面向过程编程(Process-oriented programming ):   面向过程编程是以功能为中心来进行思考和组织的一种编程方法,它强调的是系统的数据被加工和处理的过程,在程序 ...