From 575226707a502a79eb3b090b6fa50443c8922280 Mon Sep 17 00:00:00 2001 From: Tonghann Teng Date: Mon, 23 Mar 2020 23:37:27 -0500 Subject: [PATCH] 8. Spiral Matrix --- afteracademy/SpiralMatrix.java | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 afteracademy/SpiralMatrix.java diff --git a/afteracademy/SpiralMatrix.java b/afteracademy/SpiralMatrix.java new file mode 100644 index 0000000..a0da3a9 --- /dev/null +++ b/afteracademy/SpiralMatrix.java @@ -0,0 +1,58 @@ +// 8. Spiral Matrix + +class SpiralMatrix { + + public int[] spiralMatrix(int[][] arr) { + // write your awesome code here + int[] result = new int[arr.length * arr[0].length]; + int index = 0; + int top = 0; + int right = arr[0].length - 1; // 4 -1 = 3 + int down = arr.length - 1; // 3 - 1 = 2 + int left = 0; + while (index < result.length) { + for (int i = 0; i < arr[0].length; i++) { + if (isValid(arr, top, i)) { + result[index] = arr[top][i]; + arr[top][i] = 0; + index++; + } + } + top++; + for (int i = 0; i < arr.length; i++) { + if (isValid(arr, i, right)) { + result[index] = arr[i][right]; + arr[i][right] = 0; + index++; + } + } + right--; + for (int i = arr[0].length - 1; i >= 0; i--) { + if (isValid(arr, down, i)) { + result[index] = arr[down][i]; + arr[down][i] = 0; + index++; + } + } + down--; + for (int i = arr.length - 1; i >= 0; i--) { + if (isValid(arr, i, left)) { + result[index] = arr[i][left]; + arr[i][left] = 0; + index++; + } + } + left++; + } + return result; + } + + private boolean isValid(int[][] arr, int row, int col) { + return (row >= 0) && + (row < arr.length) && + (col >= 0) && + (col < arr[0].length) && + arr[row][col] != 0; + } + +} \ No newline at end of file