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. HDU 4818 Golden Radio Base (2013长春现场赛B题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 进制转换. 现场根据题目给的两个公式,不断更新!!! 胡搞就可以了. 现场3A,我艹,一次循环开 ...

  2. High accuracy voltage regulator

    High accuracy voltage regulator Good morning everybody, I want to make a accurate voltage regulator ...

  3. WINUSB Descriptors

    Reading string descriptors: String (0x01): "Travis Robinson" String (0x02): "Benchmar ...

  4. Git 代码更新:git fetch 和 git pull 的区别

    Git 从远程的分支获取最新的版本到本地有这样 2 个命令: 1. git fetch:相当于是从远程获取最新版本到本地,但不会自动 merge git fetch origin master git ...

  5. 飘逸的python - 中文编码长度有趣的现象

    最近在做验证用户姓名的功能时发现这样一个现象.   >>len(u'打怪者') #unicode 3 >>len(u'打怪者'.encode('gbk')) #gbk 6 &g ...

  6. delphi 服务程序

    http://www.delphifans.com/InfoView/Article_662.html 用Delphi创建服务程序 Windows 2000/XP和2003等支持一种叫做"服 ...

  7. java之 ------ 文件拷贝

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

  8. Java File.separator

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...

  9. 解决javamail ssl 测试unable to find valid certification path to requested target

    运行 java InstallCert smtp.interdrp.com:465 得到jssecacerts文件后复制到jdk1.6.0_14\jre\lib\security目录 然后再发送邮件就 ...

  10. 高速Android开发系列通信篇之EventBus

    概述及基本概念 **EventBus**是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间的通信.比方请求网络,等网络返回时通过Hand ...