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. 积累的VC编程小技巧之编辑框

    1.如何让对话框中的编辑框接收对话框的消息 ////////////////////////////////////////////////// 如何让对话框中的CEdit控件类接收对话框的消息/// ...

  2. SQLite/嵌入式数据库

    SQLite/嵌入式数据库 的项目要么不使用数据库(一两个文配置文件就可以搞定),要么就会有很多的数据,用到 postgresql,操练sqlite的还没有.现在我有个自己的小测试例子,写个数据库对比 ...

  3. 框架学习之道:PE框架简介

    1.PE框架开发新功能所需的部分 2.PE框架工作流程(重要) 首先根据<transcation>中的id号,找到模板(template),然后再根据模板找到责任链(chain),一旦确认 ...

  4. Cocos2d-x 创建(create)动画对象CCAnimation报错分析

    本人在使用精灵表单创建动画的过程中突然遇到了一些个问题,下面进行一下分析总结. 根据在Cocos2d-iphone中的经验,我写出了如下的代码: CCSpriteFrameCache::sharedS ...

  5. delphi 自我删除和线程池(1000行代码,需要仔细研究)

    unit Unit4; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. [Android学习笔记]双缓冲绘图技术

    双缓冲技术绘图: 什么情况下产生的双缓冲技术?当数据量很大时,绘图可能需要花费很长的时间,这样屏幕就会出现卡顿,闪烁等现象. 什么是双缓冲技术?双缓冲是在内存中创建一个与屏幕绘制区域一致的对象,先将图 ...

  7. python中逐行读取文件的最佳方式_Drupal_新浪博客

    python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式    (2010-08-18 15:59:28)    转载▼    标签:    python   ...

  8. Easyui 让Window弹出居中

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

  9. git digest

    .gitignore文件示例: .classpath .project .idea/ .settings/ target/ *~ *.iml *.log *.tmp https://zhuanlan. ...

  10. Linux SSH常用总结(转)

    一.连接到远程主机 格式: ssh name@remoteserver 例如: ssh ickes@192.168.27.211 二.连接到远程主机指定的端口 格式: ssh name@remotes ...