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. find 查询文件,执行文件

    使用find查询文件批量处理文件: Find . -mtime +2 -name “*.log.*” -exec rm -f {} \; Find参数: Find : 查找 . : 当前目录 -mti ...

  2. 2019.03.09 bzoj5371: [Pkusc2018]星际穿越(主席树)

    传送门 题意简述: 给一个序列,对于第iii个位置,它跟[limi,i−1][lim_i,i-1][limi​,i−1]这些位置都存在一条长度为111的无向边. 称dist(u,v)dist(u,v) ...

  3. Python_day8

    多态 class Animal(object): def run(self): print('animal is running') class Dog(Animal): def run(self): ...

  4. 将Paul替换成Ringo

    <!DOCTYPE html><html><head lang="en">  <meta charset="UTF-8" ...

  5. 数据库的Connection、Cursor两大对象

    Python 数据库图解流程 Connection.Cursor比喻 Connection()的参数列表 host,连接的数据库服务器主机名,默认为本地主机(localhost). user,连接数据 ...

  6. 【慕课网实战】八、以慕课网日志分析为例 进入大数据 Spark SQL 的世界

    用户行为日志:用户每次访问网站时所有的行为数据(访问.浏览.搜索.点击...)     用户行为轨迹.流量日志   日志数据内容: 1)访问的系统属性: 操作系统.浏览器等等 2)访问特征:点击的ur ...

  7. Java 包与类的命名(util、service、tool、dao )区别

    util 通用的.与业务无关的,可以独立出来,可供其他项目使用.方法通常是public static,一般无类的属性,如果有,也是public static. service 与某一个业务有关,不是通 ...

  8. mpvue 初体验之改写车标速查小程序

    前文 说到我开发了一个简单的小程序叫做 车标速查(代码以及二维码详见 这里),本文简单讲讲如何将这个小程序转为 mpvue 开发(最终 成果 ) mpvue 官网的 文档 真的是非常简单,不,应该说是 ...

  9. 写给笨蛋徒弟的学习手册(3)—C#中15个预定义数据类型

    在C#中学习中,你会很早的遇到预定义数据类型这个概念,但你有没有仔细想过它存在的意义?正所谓“存在即合理”,预定义数据类型的存在目的主要有俩个方面,一是为了增加程序的安全性,同时减轻编译器负担,加快编 ...

  10. selenium自动化常用函数

    前段时间弄一个测试框架,满足公司简单网站的测试,整合了一个函数模块,包括常用的截图.邮件发送.测试报告生成,具体代码如下 import smtplib from BSTestRunner import ...