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 Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
int m = matrix.length;
if(m == 0) return res;
int n = matrix[0].length;
//循环次数小于m/2和n/2
for(int i = 0; i < m/2 && i < n/2; i++){
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[i][i+j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[i+j][n-i-1]);
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[m-i-1][n-i-1-j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[m-i-1-j][i]);
}
//循环结束后假设行数/列数是奇数,则还剩一行/列
if(m % 2 != 0 && m <= n){
for(int j = 0; j < n - (m/2) * 2; j++)
res.add(matrix[m/2][m/2+j]);
}
else if(n % 2 != 0 && m > n){
for(int j = 0; j < m - (n/2) * 2; j++)
res.add(matrix[n/2+j][n/2]);
}
return res;
}
}

Leetcode: Spiral Matrix. Java的更多相关文章

  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 54

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

  5. LeetCode: Spiral Matrix 解题报告

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

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

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

随机推荐

  1. 熬之滴水穿石:JSP--HTML中的JAVA代码(6)

                                                                       39--JSTL 在JSP编码中需考虑的一种方法,因为这种方法可以 ...

  2. Floodlight 处理交换机增加/移除过程

         Floodlight 使用的是Netty架构,在Controller.java 入口函数中显示创建ServerBootstrap,设置套接字选项,ChannelPipeline,此时监听套接 ...

  3. 谈VC++对象模型

    一个C++程序员,想要进一步提升技术水平的话,应该多了解一些语言的语意细节.对于使用VC++的程序员来说,还应该了解一些VC++对于C++的诠释.Inside the C++ Object Model ...

  4. hdu2159 Fate 二维背包

    #include <cstring> #include <string> #include <cstdio> #include <cmath> #inc ...

  5. hibernate学习(二)

    hibernate 单向一对多映射 一.数据表设计 数据库名:hibernate5 数据表: ①表名:CUSTOMERS 字段: CUSTOMER_ID  CUSTOMER_NAME ②表名:ORDE ...

  6. 摘要算法CRC8、CRC16、CRC32,MD2 、MD4、MD5,SHA1、SHA256、SHA384、SHA512,RIPEMD、PANAMA、TIGER、ADLER32

    1.CRC8.CRC16.CRC32 CRC(Cyclic Redundancy Check,循环冗余校验)算法出现时间较长,应用也十分广泛,尤其是通讯领域,现在应用最多的就是 CRC32 算法,它产 ...

  7. Easyui 让Window弹出居中

    原文:Easyui 让Window弹出居中 easyui1.3.2版本,window的弹出不会居中了.而dialog是会居中的,我们必须为为window的open事件做扩展 代码如下:只要加入以下代码 ...

  8. [Cocos2d-x]节点之间的相互通讯

    在做.NET开发时,对象之间的相互通讯一般使用事件(event)实现,事件概念是.NET对Delegate的封装. 在Cocos2d-x开发过程中,对象之间的通讯刚开始时不知道如何实现,于是想到c++ ...

  9. JSON-C 的安装与使用

    下载源代码安装步骤 wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz tar xvf json-c-0.9.tar.gz cd jso ...

  10. 在java代码中进行px与dip(dp)、px与sp单位值的转换

        其实都是以前保存的代码,最近发现自己的资料库很混乱,索性都整理成博客,方便以后自己要用的时候快速找到. DisplayUtil.java /** * 单位转换工具 * * @author ca ...