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. P3806 【模板】点分治1

    一道淀粉质的模版题,开始是暴力 #include <bits/stdc++.h> #define up(i,l,r) for(register int i = (l); i <= ( ...

  2. 找出数组中最大值and索引

    找出数组中的最大值和和最大值的索引位置..... 第一中方法: /** * 找出数组中最大值和最大值的索引 * @param args */ public static void main(Strin ...

  3. 今天我给你们推荐一本书《Linux就该这么学》!!!

    本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国内最值得去读的Linux教材,也是最有价值 ...

  4. 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)

    ---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...

  5. bond绑定两张物理网卡为一张逻辑网卡

    问题:cnetos7同时接入两个独立网络,但两个网络的IP网段相同时只能路由到一个网络 解决方法:使用bond绑定两张物理网卡为一张逻辑网卡 1.新建文件bond.conf,内容如下 alias bo ...

  6. docker + spring boot 打包 部署。

    docker 安装 什么的 就不一一介绍了 不会安装百度一找一堆. 我这直接上代码. 首先你要有个spring boot项目. 然后打包.打包很简单 我打包的是 jar文件.直接在pom.xml文件里 ...

  7. vue el-upload form 同时提交

    项目需要form 表单和文件上传同时在一个请求,废话不多说上代码: 上传的组件使用pug格式 .row.my-4 .col-12 el-form(:model='domain', :rules='va ...

  8. 阿里云服务器搭建SS代理教程!!!

    二.搭建教程 1.环境介绍 阿里云服务器ECS(香港): 配置:cpu 1核心.内存 1GB.出网带宽 10Mbps. 系统:CentOS 7.4 64位 2.服务器端搭建 1)使用root用户,分别 ...

  9. App性能测试之启动时间(安卓)手动+脚本

    这个测试可以使用adb工具,adb的安装方式 测试策略 安装后首次启动 常规冷启动 热启动(一般这个都很少测试) 针对1和2的测试方法 步骤1:在cmd中输入如下命令 adb logcat * > ...

  10. django2+python3+uwsgi+centos7部署网站

    Centos7中安装Python虚拟环境 2018年08月27日 00:09:36 kaichenkai 阅读数:984 1.为什么要搭建虚拟环境? 问题:如果在一台电脑上, 想开发多个不同的项目, ...