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. TControlStyle.csParentBackground的作用(附Delphi里的所有例子,待续)

    Only applicable when Themes are enabled in applications on Windows XP. Causes the parent to draw its ...

  2. 移动开发的框架(用Firepower,不用listview,超快) good

    我是通过http传送xml后台是阿帕奇的http server,后台可以用delphi或php 都可以.用post 刚才试了试自带的TNetHttpClient,感觉还好,代码封装也不算深,收发数据也 ...

  3. 程序猿的量化交易之路(26)--Cointrader之Listing挂牌实体(13)

    转载须注明出处:http://blog.csdn.net/minimicall? viewmode=contents,http://cloudtrade.top Listing:挂牌. 比方某仅仅股票 ...

  4. eclipse如何查看类之间的引用关系

    今天遇到这个问题:mark一点点: 在类名上单击右键.选择Reference->Workingspace快捷克债券Ctrl+Shift+G 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  5. JSCapture实现屏幕捕捉

    JSCapture 是用纯 JavaScript 和 HTML5 实现的屏幕捕捉库. 能够随意在浏览器或者桌面视频进行截图, JSCapture 使用 getUserMedia 来实现屏幕捕获. 当前 ...

  6. Mqtt协议IOS移植完1

    MQTTClient.h #import <Foundation/Foundation.h> @protocol MQTTDelegate <NSObject> /** * @ ...

  7. ie浏览器提交参数和其它浏览器的区别

    场景描述: 用户注册模块(ajax提交方式,post方法),在url后追加了一个参数,如:url+‘btnvalue=中文参数’,如此在非ie浏览器注册时,功能完好,但在ie下注册不成功.调式后发现在 ...

  8. 11gR2RAC环境DBCA创建一个数据库错误ORA-15055 ORA-15001

    11gR2RAC环境DBCA创建一个数据库错误ORA-15055 ORA-15001 象: 在11gR2 GridInfrastructure和Database软件安装完毕之后,运行DBCA创建数据库 ...

  9. PVPlayer的实现方式

    关于opencore下多媒体播放,在mediaserver进程里面仅仅有一行代码: MediaPlayerService::instantiate(); 这行代码的作用是初始化一个MediaPlaye ...

  10. 【C语言】写一个函数,实现字符串内单词逆序

    //写一个函数,实现字符串内单词逆序 //比如student a am i.逆序后i am a student. #include <stdio.h> #include <strin ...