[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 ...
随机推荐
- .net core在Linux ARM板上运行
最近接了个临时任务,给别的项目组的机器人平台上开发个小程序,那机器人上跑的是ARM平台,ubuntu的系统. 本来打算用C++写的,由于最近用.net core较多,鉴于其在linux平台良好的兼容性 ...
- [Go] defer 语句
Go 还有一些特有的流程控制语句,其中一个就是 defer 语句.该语句用于延迟调用指定的函数,它只能出现在函数的内部,由 defer 关键字以及针对某个函数的调用表达式组成.这里被调用的函数称为 延 ...
- 使用 NuGet 管理我们的程序集 - 预发行版
1.缘起 在我们的项目中.须要引用的组件统一放在一个 Libs 文件夹下.不管对于平台上的公共组件.还是应用模块,都是如此. 假设一个应用模块,比如能源管理(EM).要引用平台提供的公共组件,比如数据 ...
- Revit MEP API连接器类别
连接器的类别,风管不仅有两端,可能在曲线上也有. ; ; ; ; Connector conn = csi.Current ; ...
- In House打包流程
在一个app历经数周持续开发和多个版本快速内部迭代之后,当我们需要把这个版本发布到我们实际应用场景中,面对我们真实用户去say hi时,如果自身产品在发布(内测版本)之前确实找到一些潜在切相对稳定的种 ...
- C#编程(三十三)----------Array类
Array类 创建数组 Array intArray1 = Array.CreateInstance(typeof(int), 5); for (int i = 0; i < 5; i++) { ...
- Snail—UI学习之得到某组件的方法
第一种方法:依据传入函数的參数对象的tag属性区分 比方 多个button运行同一个方法,可是不同的方法运行时.里面的逻辑又不一样 那就得加以区分 这时能够用tag来差别 //再新建一个Button ...
- go 用的不多的命令
8.go doc 文档注释相关,可以搭建本地GO文档服务器,包含自己的项目注释,更多细节请参考:https://github.com/hyper-carrot/go_command_tutorial/ ...
- 通过AnimationSet设置动画
在代码中可以通过set来设置多个动画属性,这里分开来设置不同的属性. 首先先贴上布局文件,里面的imageview是用来做动画的控件 <RelativeLayout xmlns:android= ...
- [Hook] 跨进程 Binder 学习指南
cp from : http://weishu.me/2016/01/12/binder-index-for-newer/ 毫不夸张地说,Binder是Android系统中最重要的特性之一:正如其名“ ...