From df4ee95c2f5a8f0013519196b71b92e9cf99fcbc Mon Sep 17 00:00:00 2001 From: Kevin Naughton Jr Date: Tue, 5 Jun 2018 10:05:58 -0400 Subject: [PATCH] added SpiralMatrix.java --- company/google/SpiralMatrix.java | 61 +++++++++++++++++++++++++++++ company/microsoft/SpiralMatrix.java | 61 +++++++++++++++++++++++++++++ company/uber/SpiralMatrix.java | 61 +++++++++++++++++++++++++++++ leetcode/array/SpiralMatrix.java | 61 +++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 company/google/SpiralMatrix.java create mode 100644 company/microsoft/SpiralMatrix.java create mode 100644 company/uber/SpiralMatrix.java create mode 100644 leetcode/array/SpiralMatrix.java diff --git a/company/google/SpiralMatrix.java b/company/google/SpiralMatrix.java new file mode 100644 index 0000000..15db885 --- /dev/null +++ b/company/google/SpiralMatrix.java @@ -0,0 +1,61 @@ +//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. +// +//Example 1: +// +//Input: +//[ + //[ 1, 2, 3 ], + //[ 4, 5, 6 ], + //[ 7, 8, 9 ] +//] +//Output: [1,2,3,6,9,8,7,4,5] +//Example 2: +// +//Input: +//[ + //[1, 2, 3, 4], + //[5, 6, 7, 8], + //[9,10,11,12] +//] +//Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList(); + if(matrix == null || matrix.length == 0) { + return result; + } + + int rowStart = 0; + int rowEnd = matrix.length - 1; + int colStart = 0; + int colEnd = matrix[0].length - 1; + while(rowStart <= rowEnd && colStart <= colEnd) { + for(int i = colStart; i <= colEnd; i++) { + result.add(matrix[rowStart][i]); + } + rowStart++; + + for(int i = rowStart; i <= rowEnd; i++) { + result.add(matrix[i][colEnd]); + } + colEnd--; + + if(rowStart <= rowEnd) { + for(int i = colEnd; i >= colStart; i--) { + result.add(matrix[rowEnd][i]); + } + } + rowEnd--; + + if(colStart <= colEnd) { + for(int i = rowEnd; i >= rowStart; i--) { + result.add(matrix[i][colStart]); + } + } + colStart++; + } + + return result; + } +} diff --git a/company/microsoft/SpiralMatrix.java b/company/microsoft/SpiralMatrix.java new file mode 100644 index 0000000..15db885 --- /dev/null +++ b/company/microsoft/SpiralMatrix.java @@ -0,0 +1,61 @@ +//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. +// +//Example 1: +// +//Input: +//[ + //[ 1, 2, 3 ], + //[ 4, 5, 6 ], + //[ 7, 8, 9 ] +//] +//Output: [1,2,3,6,9,8,7,4,5] +//Example 2: +// +//Input: +//[ + //[1, 2, 3, 4], + //[5, 6, 7, 8], + //[9,10,11,12] +//] +//Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList(); + if(matrix == null || matrix.length == 0) { + return result; + } + + int rowStart = 0; + int rowEnd = matrix.length - 1; + int colStart = 0; + int colEnd = matrix[0].length - 1; + while(rowStart <= rowEnd && colStart <= colEnd) { + for(int i = colStart; i <= colEnd; i++) { + result.add(matrix[rowStart][i]); + } + rowStart++; + + for(int i = rowStart; i <= rowEnd; i++) { + result.add(matrix[i][colEnd]); + } + colEnd--; + + if(rowStart <= rowEnd) { + for(int i = colEnd; i >= colStart; i--) { + result.add(matrix[rowEnd][i]); + } + } + rowEnd--; + + if(colStart <= colEnd) { + for(int i = rowEnd; i >= rowStart; i--) { + result.add(matrix[i][colStart]); + } + } + colStart++; + } + + return result; + } +} diff --git a/company/uber/SpiralMatrix.java b/company/uber/SpiralMatrix.java new file mode 100644 index 0000000..15db885 --- /dev/null +++ b/company/uber/SpiralMatrix.java @@ -0,0 +1,61 @@ +//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. +// +//Example 1: +// +//Input: +//[ + //[ 1, 2, 3 ], + //[ 4, 5, 6 ], + //[ 7, 8, 9 ] +//] +//Output: [1,2,3,6,9,8,7,4,5] +//Example 2: +// +//Input: +//[ + //[1, 2, 3, 4], + //[5, 6, 7, 8], + //[9,10,11,12] +//] +//Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList(); + if(matrix == null || matrix.length == 0) { + return result; + } + + int rowStart = 0; + int rowEnd = matrix.length - 1; + int colStart = 0; + int colEnd = matrix[0].length - 1; + while(rowStart <= rowEnd && colStart <= colEnd) { + for(int i = colStart; i <= colEnd; i++) { + result.add(matrix[rowStart][i]); + } + rowStart++; + + for(int i = rowStart; i <= rowEnd; i++) { + result.add(matrix[i][colEnd]); + } + colEnd--; + + if(rowStart <= rowEnd) { + for(int i = colEnd; i >= colStart; i--) { + result.add(matrix[rowEnd][i]); + } + } + rowEnd--; + + if(colStart <= colEnd) { + for(int i = rowEnd; i >= rowStart; i--) { + result.add(matrix[i][colStart]); + } + } + colStart++; + } + + return result; + } +} diff --git a/leetcode/array/SpiralMatrix.java b/leetcode/array/SpiralMatrix.java new file mode 100644 index 0000000..15db885 --- /dev/null +++ b/leetcode/array/SpiralMatrix.java @@ -0,0 +1,61 @@ +//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. +// +//Example 1: +// +//Input: +//[ + //[ 1, 2, 3 ], + //[ 4, 5, 6 ], + //[ 7, 8, 9 ] +//] +//Output: [1,2,3,6,9,8,7,4,5] +//Example 2: +// +//Input: +//[ + //[1, 2, 3, 4], + //[5, 6, 7, 8], + //[9,10,11,12] +//] +//Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList(); + if(matrix == null || matrix.length == 0) { + return result; + } + + int rowStart = 0; + int rowEnd = matrix.length - 1; + int colStart = 0; + int colEnd = matrix[0].length - 1; + while(rowStart <= rowEnd && colStart <= colEnd) { + for(int i = colStart; i <= colEnd; i++) { + result.add(matrix[rowStart][i]); + } + rowStart++; + + for(int i = rowStart; i <= rowEnd; i++) { + result.add(matrix[i][colEnd]); + } + colEnd--; + + if(rowStart <= rowEnd) { + for(int i = colEnd; i >= colStart; i--) { + result.add(matrix[rowEnd][i]); + } + } + rowEnd--; + + if(colStart <= colEnd) { + for(int i = rowEnd; i >= rowStart; i--) { + result.add(matrix[i][colStart]); + } + } + colStart++; + } + + return result; + } +}