【数组】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]
.
思路:
逐个环的打印, 对于m *n的矩阵,环的个数是 Math.ceil((Math.min(m,n))/2)。对于每个环顺时针打印四条边。
注意的是:最后一个环可能只包含一行或者一列数据
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
if(matrix.length==0||matrix==null){
return [];
}
var m=matrix.length,n=matrix[0].length;
var circle=Math.ceil((Math.min(m,n))/2); var a=m,b=n,res=[];
for(var i=0;i<circle;i++,a-=2,b-=2){
for(var col=i;col<i+b;col++){
res.push(matrix[i][col]);
}
for(var row=i+1;row<i+a;row++){
res.push(matrix[row][i+b-1]);
}
if(a==1||b==1)break;
for(var col=i+b-2;col>=i;col--){
res.push(matrix[i+a-1][col]);
}
for(var row=i+a-2;row>i;row--){
res.push(matrix[row][i]);
} } return res; };
【数组】Spiral Matrix的更多相关文章
- [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 - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- Spiral Matrix
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- PAT1105:Spiral Matrix
1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- A1105. Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- 1105 Spiral Matrix
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
随机推荐
- js函数预编译
function fn(a){ console.log(a); var a = 123; function a(){} console.log(a); var b = function(){} con ...
- python编码(七)
本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854:2. UTF-8,E59388:3. GBK,B9FE. 一.python中的s ...
- 5) mvn archetype:generate
获取帮助 mvn -h 命令格式 usage: mvn [options] [<goal(s)>] [<phase(s)>] -D,--define <arg> D ...
- (匹配 二维建图) Antenna Placement --POJ --3020
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82834#probl ...
- hbase使用MapReduce操作3(实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中)
Runner类 实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中. package com.yjsj.hbase_mr; import org.apache.hadoo ...
- Delphi 中 FindWindow 和 FindWindowEx 的语法和用法
FindWindow(lpClassName, {窗口的类名}lpWindowName: PChar {窗口的标题}): HWND; {返回窗口的句柄; 失败返 ...
- Android SDK目录结构
Android版本下载:从4.0到8.0版本: Android SDK目录结构图: sdk全称:software develop kits 软件开发工具集 add-ons:Google API map ...
- DAC--解决windows验证无法登陆的问题
解决思路: 使用单用户管理员模式启动SQL Server,再使用SQLCMD连接上数据库,此时有sysadmin权限,添加用户并赋予相应权限 1>停止SQL Server服务运行 2>在C ...
- App.config使用方法(基础教程)
WPF程序中的App.config文件是应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,是一种很高效的程序配置方式. 1.首先在工程中配 ...
- JAVA 从头开始<三>
一.数据类型转换 取反:1变0,0变1 强转 Insteger.toBinaryString(-7); 下面这样写会出错,要用l来接收 为什么byte b 可以接收int类型(而不是10b),大数据类 ...