@Test public void shouldReturnEmptyArrayIfMatrixIsEmpty() { int[][] matrix = { }; int[] result = goThroughMatrixInSpiral.go(matrix); assertEquals(0, result.length); }
public int[] go(int[][] matrix) { if (matrix == null) { throw new IllegalArgumentException("You can't pass a null insantece as input."); } if (matrix.length == 0) { return new int[0]; } int m = matrix.length; int n = matrix[0].length; int resultIndex = 0; int[] result = new int[m * n]; int x = 0; int y = 0; while (m > 0 && n > 0) { if (m == 1) { for (int i = 0; i < n; i++) { result[resultIndex++] = matrix[x][y]; y++; } break; } else if (n == 1) { for (int i = 0; i < m; i++) { result[resultIndex++] = matrix[x][y]; x++; } break; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y++; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x++; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y--; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x--; } x++; y++; m = m - 2; n = n - 2; } return result; }
GoThroughMatrixInSpiral { public int[] go(int[][] matrix) { if (matrix == null) { throw new IllegalArgumentException("You can't pass a null insantece as input."); } if (matrix.length == 0) { return new int[0]; } int m = matrix.length; int n = matrix[0].length; int resultIndex = 0; int[] result = new int[m * n]; int x = 0; int y = 0; while (m > 0 && n > 0) { if (m == 1) { for (int i = 0; i < n; i++) { result[resultIndex++] = matrix[x][y]; y++; } break; } else if (n == 1) { for (int i = 0; i < m; i++) { result[resultIndex++] = matrix[x][y]; x++; } break; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y++; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x++; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y--; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x--; } x++; y++; m = m - 2; n = n - 2; } return result; } }
GoThroughMatrixInSpiral { public int[] go(int[][] matrix) { if (matrix == null) { throw new IllegalArgumentException("You can't pass a null insantece as input."); } if (matrix.length == 0) { return new int[0]; } int m = matrix.length; int n = matrix[0].length; int resultIndex = 0; int[] result = new int[m * n]; int x = 0; int y = 0; while (m > 0 && n > 0) { if (m == 1) { for (int i = 0; i < n; i++) { result[resultIndex++] = matrix[x][y]; y++; } break; } else if (n == 1) { for (int i = 0; i < m; i++) { result[resultIndex++] = matrix[x][y]; x++; } break; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y++; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x++; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y--; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x--; } x++; y++; m = m - 2; n = n - 2; } return result; }  }
GoThroughMatrixInSpiral { public int[] go(int[][] matrix) { if (matrix == null) { throw new IllegalArgumentException("You can't pass a null insantece as input."); } if (matrix.length == 0) { return new int[0]; } int m = matrix.length; int n = matrix[0].length; int resultIndex = 0; int[] result = new int[m * n]; int x = 0; int y = 0; while (m > 0 && n > 0) { if (m == 1) { for (int i = 0; i < n; i++) { result[resultIndex++] = matrix[x][y]; y++; } break; } else if (n == 1) { for (int i = 0; i < m; i++) { result[resultIndex++] = matrix[x][y]; x++; } break; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y++; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x++; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y--; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x--; } x++; y++; m = m - 2; n = n - 2; } return result; }  int[] go(int[][] matrix); }
GoThroughMatrixInSpiral { public int[] go(int[][] matrix) { if (matrix == null) { throw new IllegalArgumentException("You can't pass a null insantece as input."); } if (matrix.length == 0) { return new int[0]; } int m = matrix.length; int n = matrix[0].length; int resultIndex = 0; int[] result = new int[m * n]; int x = 0; int y = 0; while (m > 0 && n > 0) { if (m == 1) { for (int i = 0; i < n; i++) { result[resultIndex++] = matrix[x][y]; y++; } break; } else if (n == 1) { for (int i = 0; i < m; i++) { result[resultIndex++] = matrix[x][y]; x++; } break; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y++; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x++; } for (int i = 0; i < n - 1; i++) { result[resultIndex++] = matrix[x][y]; y--; } for (int i = 0; i < m - 1; i++) { result[resultIndex++] = matrix[x][y]; x--; } x++; y++; m = m - 2; n = n - 2; } return result; }  int[] go(int[][] matrix);  }