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. Javascript Array和String的互转换。

    Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...

  2. 京东SSO单点登陆实现分析

    京东的sso流程: 初始访问状态: cookies: http请求:   1.在首页点击登陆,跳转至passport.360buy.com,给予验证cookie alc(可以试试在提交登陆信息前删除该 ...

  3. Uniscribe相关文章

    相关资料很少 http://msdn.microsoft.com/en-us/library/windows/desktop/dd374127(v=vs.85).aspx http://www.cnb ...

  4. Vue 插件写法

    都说Vue2简单,上手容易,但小马过河,自己试了才晓得,除了ES6语法和webpack的配置让你感到陌生,重要的是思路的变换,以前随便拿全局变量和修改dom的锤子不能用了,变换到关注数据本身.vue的 ...

  5. MyBatis-Generator最佳实践

    引用地址:http://arccode.net/2015/02/07/MyBatis-Generator%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/ 最近使用MyBati ...

  6. Xcode 5.1.1 与 Xcode 6.0.1 的共存之路(建议大家在升级Xcode 6.0.1 的时候保留Xcode 5.1.1)

    最近升级了Xcode 6.0.1 与原有项目有不少冲突.建议大家谨慎升级,同时做好备份.二者共存推荐如下帖子. http://jingyan.baidu.com/article/1612d500457 ...

  7. [CALayer release]: message sent to deallocated instance

    遇到此问题时,查看以下类的dealloc方法中是否有过度释放. 可以在release前先判断以下对象是否还存在. -(void)dealloc { if (!m_tableView) [m_table ...

  8. sql 语句注意括号配对

  9. mac 设置 代理 上网 步骤 同时设置邮件代理

    设置mac电脑网络代理步骤如下: 第一步:打开网络偏好设置 第二步:依次输入如下参数 第三步:设置代理服务器地址 设置邮箱使用代理步骤如下: 第一步: 第二步:选择使用“系统代理设置”即可,无需其它多 ...

  10. UITableView分割线样式与颜色

    tv.separatorStyle = UITableViewCellSeparatorStyleSingleLine;   //设置样式 tv.separatorColor = [UIColor c ...