[Algorithm] Print 2-D array in spiral order

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的更多相关文章
- Print a Binary Tree in Vertical Order
http://www.geeksforgeeks.org/print-binary-tree-vertical-order/ package algorithms; import java.util. ...
- [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 ...
- 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 ...
- 挑战一下吧!C#测试开发工程师英语面试题
1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size o ...
- 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 ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- 59. Spiral Matrix II (Array)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- array题目合集
414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...
- LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix
1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat ...
随机推荐
- STM32F4xx -- Cortex M4
STM32F4xx official page: http://www.st.com/internet/mcu/subclass/1521.jspIntroductionFPU - Floating ...
- [Go] Template 使用简介
Golang 提供了两个标准库用来处理模板 text/template 和 html/template.我们使用 html/template 格式化 html 字符. 模板引擎 模板引擎很多,Pyth ...
- 基于SmartThreadPool线程池技术实现多任务批量处理
一.多线程技术应用场景介绍 本期同样带给大家分享的是阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何采用基于开源组件SmartThreadPool线程池技术实现多任务批量处理.在工作中您是否 ...
- delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行。
delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行. 使用这个控件需要小心 function Tfrm_MainIPC.Open ...
- iOS内存管理策略和实践
转:http://www.cocoachina.com/applenews/devnews/2013/1126/7418.html 内存管理策略(memory Management Policy) N ...
- Oracle 导入导出 dmp 文件
导入dmp文件,需要知道这个dmp文件创建的用户.因此需要先创建用户,并授权给它. (1)用户的创建 首先,以system用户登录Oracle SQL Developer 其次,在sql工作表(可以用 ...
- Informix 常用函数
一.内部函数 1.内部合计函数 1)COUNT(*) 返回行数 2)COUNT(DISTINCT COLNAME) 返回指定列中唯一值的个数 3)SUM(COLNAME/EXPRESSION) 返回指 ...
- ubuntu下安装android sdk运行模拟器出现错误:
./emulator: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No ...
- DataGridView 在 WinForms中应用不同的单元格式
/// <summary> /// Set the cell background colour to make the ups and downs more visible. /// & ...
- 霍夫曼编码(Huffman Coding)
霍夫曼编码(Huffman Coding)是一种编码方法,霍夫曼编码是可变字长编码(VLC)的一种. 霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符 ...