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. Python Day 11

    阅读目录 内容回顾 函数的参数 函数的嵌套调用 ##内容回顾 # 什么是函数:具体特定功能的代码块 - 特定功能代码块作为一个整体,并给该整体命名,就是函数 # 函数的优点: # 1.减少代码的冗余 ...

  2. 谷歌浏览器运行Flash

    最近有人问我谷歌浏览器的flash总是要点击手动运行才可以使用.看了很多网上很多教程,并没有比较好的解决方案. 自己找了相关资料后,找到了一个比较好的完整的.特此在这边放出来给大家使用. 新建记事本, ...

  3. 剑指offer PART 2

    剑指offer PART 2 书点击自取 提取码: njku 标签(空格分隔): 笔记 C++知识点: 1.面向对象的特性 2.构造函数 3.析构函数 4.动态绑定 5.常用的设计模式 6.UML图 ...

  4. 去掉"You are running Vue in development mode"提示

    vue项目中报错: You are running Vue in development mode.Make sure to turn on production mode when deployin ...

  5. 重写toFixed()方法

    1.直接在原型上修改,仍然使用元调用方式 Number.prototype.toFixed = function (exponent) { return parseInt(this * Math.po ...

  6. 用chrome模拟微信浏览器访问页面

    有的网页通过限制 user agent  来达到禁止在微信浏览器以外的地方访问,我们通过chrome修改user agent即可破解. CHROME浏览器,进入开发者模式 切换到手机浏览模板 打开 N ...

  7. vue学习-自动行合并的table

    测试的效果 测试的html源码截图 v-table在tableGroup.js中定义,以下就render方法,行的所有单元格都在tableGrouper.js中处理 render:function(h ...

  8. android项目生成aar和引用aar解决方案

    引用依赖在项目中很常见,java开发一般是引用jar,所以android大家一般也是引用jar,但是android引用jar有时候会出现各种包冲突,很头痛!, 其实android有自己独特的引用aar ...

  9. Android中屏幕保持唤醒

    1.锁的类型 PowerManager中各种锁的类型对CPU .屏幕.键盘的影响: PARTIAL_WAKE_LOCK : 保持CPU 运转,屏幕和键盘灯有可能是关闭的. SCREEN_DIM_WAK ...

  10. VSCode插件开发全攻略(七)WebView

    更多文章请戳VSCode插件开发全攻略系列目录导航. 什么是Webview 大家都知道,整个VSCode编辑器就是一张大的网页,其实,我们还可以在Visual Studio Code中创建完全自定义的 ...