The idea to solve the problem is set five variable, first direction, we need to switch direction after we finish printing one row or one column.

dir = (dir+) % 

Then set four boundry, top, left, right, bottom:

  let results = [],
dir = , //0: left -> right, 1: top -> bottom, 2: right -> left, 3: bottom -> top
top = ,
bottom = arys.length - ,
left = ,
right = arys[].length - ;

Each time finish printing a row or a column, modify the boundry accordingly:

function PrintSpiralOrder (arys) {
let results = [],
dir = , //0: left -> right, 1: top -> bottom, 2: right -> left, 3: bottom -> top
top = ,
bottom = arys.length - ,
left = ,
right = arys[].length - ; while (top <= bottom && left <= right) {
if (dir === ) {
for (let i = left; i <= right; i++) {
results.push(arys[top][i]);
}
top++;
} else if (dir === ) {
for (let i = top; i <= bottom; i++) {
results.push(arys[i][right]);
}
right--;
} else if (dir === ) {
for (let i = right; i >= left; i--) {
results.push(arys[bottom][i]);
}
bottom--;
} else if (dir === ) {
for (let i = bottom; i >= top; i--) {
results.push(arys[i][left]);
}
left++;
} dir = (dir + ) % ;
} return results;
} const data = [
[,,,],
[,,,],
[,,,],
[,,,]
]; const res = PrintSpiralOrder(data);
console.log(res); // [ 2, 4, 6, 8, 16, 9, 8, 1, 2, 3, 2, 5, 9, 12, 5, 11 ]

[Algorithm] Print 2-D array in spiral order的更多相关文章

  1. Print a Binary Tree in Vertical Order

    http://www.geeksforgeeks.org/print-binary-tree-vertical-order/ package algorithms; import java.util. ...

  2. [Algorithm] Print All Subsets of a Set

    Let's say given a number of array, you should print out, all the subet of this array. Example: [1, 2 ...

  3. zenefits oa - sort integer array in lexographical order

    [ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ] Then the output should be: [ 1 | 100000 | 12 | 222 ...

  4. 挑战一下吧!C#测试开发工程师英语面试题

    1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size o ...

  5. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  6. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  7. 59. Spiral Matrix II (Array)

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

  8. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  9. LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix

    1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat ...

随机推荐

  1. OpAmp Voltage Follower/Regulator

    LDO Regulator High accuracy voltage regulator Vout = 2.5V * (1 + ( 5.6 / 6.8 ) ) = 4.55V Recently th ...

  2. oracle sql 高级

    1  时间   如果是从当前时间到前一个月的这个时候之间的记录总条数:   select count(1)   from uis_md_stcustom u  where firsttime betw ...

  3. .Net 垃圾回收和大对象处理 内存碎片整理

    CLR垃圾回收器根据所占空间大小划分对象.大对象和小对象的处理方式有很大区别.比如内存碎片整理 —— 在内存中移动大对象的成本是昂贵的,让我们研究一下垃圾回收器是如何处理大对象的,大对象对程序性能有哪 ...

  4. android iOS 编码问题害死人!

    android 与后端服务器进行通信时,默认使用的编码格式是asi. 而iOS与后端通信时,获取的数据到iOS端默认被utf-8进行编码.所以,我们常常出现android能够从服务器端获取到数据,但是 ...

  5. iOS非ARC内存管理摘要 - 实践型

    关于ios内存管理.在开发过程中,内存管理很重要,我简单说明一下. 1.正确用法 UIView *v = [[UIView alloc] init]; //分配后引用计数为1 [self.view a ...

  6. SQL:四舍五入和截取

    四舍五入 , ) 截取 , )

  7. Java POI 3.17导出EXCEL并下载(带进度条提示)

    导出数据 共4590条 只需要 5 秒左右,性能还算可以 我们再来测试一下 50000 条的性能...

  8. Java 与 Json的互相转换

    这几天一直在做Java解析Json数据的一个项目,因为初识json,所以很多东西都是有着懵懂的认识.这里写下我解析时遇到的问题和收获. 我解析json时用到的是json-lib包.下载地址:http: ...

  9. Oracle初级索引学习总结

    前言  索引是常见的数据库对象,建立索引的目的是为了提高记录的检索速度.它的设置好坏,使用是否得当,极大地影响数据库应用程序和Database的性能.虽然有许多资料讲索引的用法,DBA和Develop ...

  10. jdgui反编译+javac编译=无源文件改动代码

    首先我们要知道,打包好的Java程序中都是编译好的字节码文件(*.class).这些class文件会在执行的时候被载入到JVM中. 若想替换掉某一个类,那么仅仅须要将该类的源代码又一次编译然后再替换之 ...