Skip to content

Instantly share code, notes, and snippets.

@delikat
Created September 13, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delikat/6552852 to your computer and use it in GitHub Desktop.
Save delikat/6552852 to your computer and use it in GitHub Desktop.
rotation-based array spiral traversal
var spiralTraversal = function(matrix){
var width = matrix[0].length - 1;
var height = matrix.length;
var newMatrix = [];
var firstRow = matrix[0];
if (matrix.length === 1) {
return matrix[0];
}
for (width; width >= 0; width--) {
var rotatedCol = [];
for (var row = 1; row < height; row++) {
rotatedCol.push(matrix[row][width]);
}
newMatrix.push(rotatedCol);
}
firstRow.push.apply(firstRow, spiralTraversal(newMatrix));
return firstRow;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment