[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 ...
随机推荐
- NSLineBreakMode 的区别
typedef enum { UILineBreakModeWordWrap = 0, UILineBreakModeCharacterWrap, UILineBreakMod ...
- ASP.NET 2.0
http://www.cnblogs.com/linezero/p/nightlynetcore2.html
- 移植Python3到TQ2440(一)
平台 硬件:TQ2440 64MB内存 256MB NandFlash bootloader:U-Boot 2015.04 kernel:linux-4.9 Python: Python-3.6.0 ...
- 在ASP.NET MVC中实现本地化和全球化
在开发多语言网站时,我们可以为某种语言创建一个资源文件,根据浏览器所设置的不同语言偏好,让运行时选择具体使用哪个资源文件.资源文件在生成程序集的时候被嵌入到程序集. 本篇体验,在ASP.NET MVC ...
- NavigateToPageAction打开新页面
首先要加上两个命名空间 分别为: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.In ...
- Odoo(OpenERP)配置文件openerp-server.conf详解
原文地址:http://blog.csdn.net/wangnan537/article/details/42283465 [options] ; addons模块的查找路径 addons_path ...
- 查看Oracle数据库名和实例名的命令
查看数据库名 SQL> select name from v$database; NAME --------- ORCL SQL> desc v$database; 名称 ...
- 增加定时检测linux占用内存,及时清理功能
centos为了提高效率,把部分使用过的文件缓存到了内存里. 如果是这样的话,我又不需要这样的文件性能,那就可以释放.如下两个命令就可以: #sync #echo 3 > /proc/sys/v ...
- 图片通过Base64Coder编码、解码
通过这个编码类我们可以将图片转换为这个编码的字符串,上传到服务器. 这个编码是来自小马的一个范例,我看了下挺有用的.所以就放上来以备不时之需. 先说下用法: /** * 下面注释的方法是将裁剪之后的图 ...
- Java(C#)基础差异-数组
1.填充数组 Java 数组填充替换方法Arrays.fill() 举例如下: import java.util.Arrays; public class FillDemo { public stat ...