[LeetCode]Spiral Matrix 54
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的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- LeetCode——Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- [leetcode]Spiral Matrix @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...
随机推荐
- PHPCMS 详情页静态化
<?php defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_app_class('admin',' ...
- 网格视图(GridView)功能和用法
GridView用于在界面上按行.列分布的方式来显示多个组件.GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有很高的相似性,它们都是列 ...
- plsql developer日期时间格式设置
1 工具->首选项->日期/时间都使用windows格式: 2 在环境变量加入 nls_date_format=YYYY-MM-DD HH24:MI:SS nls_timestamp_fo ...
- PHP用户注册邮箱并验证激活帐号
我们在很多网站注册会员时,注册完成后,系统会自动向用户的邮箱发送一封邮件,这封邮件的内容就是一个URL链接,用户需要点击打开这个链接才能激活之前在该网站注册的帐号.激活成功后才能正常使用会员功能. 查 ...
- margin三个值
http://www.cnblogs.com/wangkongming/p/3204734.html margin标记可以带一个.二个.三个.四个参数,各有不同的含义. margin: 20px;(上 ...
- 2017了,回家前 "年末" 分享:下雨,飘雪,红包雨,碰撞球,自定义View
(本博客为原创:http://www.cnblogs.com/linguanh/) 目录: 效果展示 感想 代码拆解 开源地址 效果展示 有没有兴趣继续看下去,直接看下"颜值"是第 ...
- 在线office文档编辑NTKO使用心得
目录 前言 什么是ntko 准备工作 实战演练 总结 一.前言 Web开发中经常需要用到在线处理office文档的功能,现在市面上有一些常用的Web页面调用显示Office的控件技术,用起来很方便.有 ...
- 前端基本知识(二):JS的原始链的理解
之前一直对于前端的基本知识不是了解很详细,基本功不扎实,但是前端开发中的基本知识才是以后职业发展的根基,虽然自己总是以一种实践是检验真理的唯一标准,写代码实践项目才是唯一,但是经常遇到知道怎么去解决这 ...
- 《无所不能的JavaScript编程系列:arguments 参数对象》
前言:无所不能的JavaScript JavaScript起源于Netscape公司的LiveScript语言,这是一种基于对象和事件驱动的客户端脚本语言,最初的设计是为了检验HTML表单输入的正确性 ...
- java线程之多个生产者消费者
温故一下上一节所学习的生产者消费者代码: 两个线程时: 通过标志位flag的if判断和同步函数互斥较好解决两个线程,一个生产者.一个消费者交替执行的功能 类名:ProducterConsumerDem ...