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. 别人的Linux私房菜(14)Linux账号管理和ACL权限设置

    用户标识符UID.GID 用户的账号信息,主要是指UID对应.组和GID对应 检查系统中是否存在用户bin:id bin 登录shell验证账号密码的步骤:找到/etc/passwd核对是否存在账号, ...

  2. 别人的Linux私房菜(13)学习Shell脚本

    CentOS6.x以前版本的系统服务启动接口在/etc/init.d/目录下,存放了脚本. Shell脚本因调用外部命令和bash 的一些默认工具,速度较慢,不适合处理大量运算. 执行方式有:直接命令 ...

  3. 字符串、数组、对象常用API

    常用的字符串API  1.常见方法和属性 length 属性,获取字符串的字符数量 charAt(i) 返回给定位置的字符 charCodeAt( ) 返回给定位置的字符的字符编码 <scrip ...

  4. 非交互式一句话添加root用户

    useradd -p `openssl passwd -1 -salt ‘lsof’ admin123` -u 0 -o -g root -G root -s /bin/bash -d /usr/bi ...

  5. maven 编码 UTF-8 的不可映射字符

    maven编译时报错,后面发现代码是用GBK编码编写,maven默认是用utf-8来编译.修改pom.xml <build> <plugins> <plugin> ...

  6. spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程

    RequestMappingHandlerMapping getMappingForMethod RequestMappingHandlerMapping 继承于 AbstractHandlerMet ...

  7. ubuntu安装spyder和jupyter notebook

    ubuntu安装spyder和jupyter notebook 安装spyder 安装spyder sudo apt install spyder sudo apt install spyder3 安 ...

  8. Python的基本数据类型

    数据类型常用函数 type(a)-得到变量a的数据类型 isinstance(a,str)-判断a是否是字符串类型 Python中有五个标准数据类型 数字Number 字符串String 数组List ...

  9. tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案

    tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案   一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...

  10. day_5字符串和列表的各种操作方法

    字符串类型: 字符串的定义是可以有多种引号嵌套 定义字符串是以开头的引号然后匹配和第一个引号相同的引号,所以当字符串中间出现和第一个引号相同的引号就会出错,这个时候就可以选择别的引号进行创建字符串,或 ...