54.Spiral Matrix

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

好久没做题了,博客也好久没更新,今天来一发水题,话说这题有点像poj的滑雪题.

传送门POJ:http://poj.org/problem?id=1088

题解:http://www.cnblogs.com/dick159/p/5258943.html

先说一下这题吧,就是分四个方向,然后从外到里按照顺序遍历,要注意结束条件.(我设置成访问过的值就不访问了)

ps:要判断一下间隔了的2个方向是否发生过移动,如果没有发生过移动则表示遍历已完毕.(这里我就用list的大小是否发生了变化来判断.)

目前没有想到更好的方法。【太笨了(╯﹏╰)】

Java Code:

public class Solution {

        public List<Integer> spiralOrder( int[][] matrix ) {
List<Integer> list = new ArrayList<Integer>();
if( matrix == null || matrix.length == 0 )
return list;
int m = matrix[ 0 ].length;
int n = matrix.length;
if( matrix[ 0 ].length == 1 ) {
for( int i = 0; i < n; i++ ) {
list.add( matrix[ i ][ 0 ] );
}
return list;
} if( matrix.length == 1 ) {
for( int i = 0; i < m; i++ ) {
list.add( matrix[ 0 ][ i ] );
}
return list;
}
int l = 0;
int preSize = 0;
int i = -1;
while( matrix[ l ][ l ] != -Integer.MAX_VALUE ) {
preSize = list.size();
for( i = l; i < m - l; i++ ) {
list.add( matrix[ l ][ i ] );
matrix[ l ][ i ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = l + 1; i < n - l; i++ ) {
list.add( matrix[ i ][ m - l - 1 ] );
matrix[ i ][ m - l - 1 ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = m - l - 2; i >= l; i-- ) {
list.add( matrix[ n - l - 1 ][ i ] );
matrix[ n - l - 1 ][ i ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = n - l - 2; i >= l + 1; i-- ) {
list.add( matrix[ i ][ l ] );
matrix[ i ][ l ] = -Integer.MAX_VALUE;
}
l++;
}
return list;
}
}

[LeetCode]Spiral Matrix 54的更多相关文章

  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 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  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 解题报告

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

  6. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  7. LeetCode——Spiral Matrix

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  8. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  9. [leetcode]Spiral Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. NodeMCU之旅(二):断线自动重连,闪烁连接状态

    事件监听器 NodeMCU采用了事件响应的方式.也就是说,只需为事件设置一个回调函数,当事件发生时,回调函数就会被调用. 注册事件监听器 wif.sta.eventMonReg() 开始监听 wifi ...

  2. Canvas 颜色反转

    ImageData中的元素反转颜色 255-data[i,i+1,i+2] <!DOCTYPE html> <html lang="en"> <hea ...

  3. nginx的配置服务器集群,负载均衡

    在server{}前配置服务器ip和端口号 如: upstream local_tomcat { local_tomcat为访问路径,在下面配置服务器ip及端口号,也可以分配权重(weight==?) ...

  4. 私有云存储搭建(owncloud)

    第一步.搭建LAMP(基于linux7.1.1503) 1 配置yum(网络加本地,下面为网络) [vault.centos.org_7.1.1503_os_x86_64_] name=added f ...

  5. 基于ssh反向代理实现的远程协助

    本文描述了怎么通过ssh反向代理实现远程协助,并提供了相关代码. 可满足web开启远程协助功能后,维护人员能够通过ssh和http登录客户机器(包括在nat环境下) web开启该功能后,ssh才能登录 ...

  6. js原生轮播图

    轮播图是新手学前端的必经之路! 直接上代码! <!DOCTYPE html><html lang="en"><head> <meta ch ...

  7. 法国总统放大招,用“分身术”竞选总统 全息3d 网

    编辑:大熊 [摘要]法国总统采用全息技术实现"分身"演讲,可谓是一次演讲,全面覆盖! 全息3d网讯:众所周知,欧美国家的总统是通过公开竞选得到的,所以能更直接.更广泛的近距离接触民 ...

  8. 游戏音频技术备忘 (四) Wwise Unreal Engine 集成代码浅析 (一)

    在Engine\Plugins\Wwise\Source下为主要Wwise相关代码,AkAudio文件夹下为运行时相关代码,AudiokineticTools下为编辑器工具相关代码,Audiokine ...

  9. c# 应用NPOI 获取Excel中的图片,保存至本地的算法

    要求:读取excel中的图片,保存到指定路径 思路:  利用NPOI中 GetAllPictures()方法获取图片信息 步骤: 1.新建一个Windows窗体应用程序 2.桌面新建一个excel,贴 ...

  10. 从并发处理谈PHP进程间通信(一)外部介质

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...