@Test public final void testSpiralMatrix() { int[][] expecteds = new int[1][1]; expecteds[0][0] = 1; int n = 1; int[][] actuals = Matrix.SpiralMatrix(n); for (int i = 0; i < n; i++) { Assert.assertArrayEquals(expecteds[i], actuals[i]); } n = 2; actuals = Matrix.SpiralMatrix(n); expecteds = new int[][]{{1,2},{4,3}}; for (int i = 0; i < n; i++) { Assert.assertArrayEquals(expecteds[i], actuals[i]); } n = 3; actuals = Matrix.SpiralMatrix(n); expecteds = new int[][]{{1,2,3},{8,9,4},{7,6,5}}; for (int i = 0; i < n; i++) { Assert.assertArrayEquals(expecteds[i], actuals[i]); } n = 5; actuals = Matrix.SpiralMatrix(n); expecteds = new int[][]{{1,2,3,4,5},{16,17,18,19,6},{15,24,25,20,7},{14,23,22,21,8},{13,12,11,10,9}}; for (int i = 0; i < n; i++) { Assert.assertArrayEquals(expecteds[i], actuals[i]); } }
static int[][] SpiralMatrix(int n) { int[][] a = new int[n][n]; int c = 1, x = 0, y = 0; int d = 0; while (c <= n * n) { if (a[x][y] == 0) { a[x][y] = c++; } else { d++; d = d % 4; } switch (d) { case 0: if (y < n - 1 && a[x][y + 1] < 1) y++; else { d = 1; x++; } break; case 1: if (x < n - 1 && a[x + 1][y] < 1) x++; else { y--; d = 2; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; d = 3; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; d = 0; } break; default: break; } } return a; }
Matrix { static int[][] SpiralMatrix(int n) { int[][] a = new int[n][n]; int c = 1, x = 0, y = 0; int d = 0; while (c <= n * n) { if (a[x][y] == 0) { a[x][y] = c++; } else { d++; d = d % 4; } switch (d) { case 0: if (y < n - 1 && a[x][y + 1] < 1) y++; else { d = 1; x++; } break; case 1: if (x < n - 1 && a[x + 1][y] < 1) x++; else { y--; d = 2; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; d = 3; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; d = 0; } break; default: break; } } return a; } }
Matrix { static int[][] SpiralMatrix(int n) { int[][] a = new int[n][n]; int c = 1, x = 0, y = 0; int d = 0; while (c <= n * n) { if (a[x][y] == 0) { a[x][y] = c++; } else { d++; d = d % 4; } switch (d) { case 0: if (y < n - 1 && a[x][y + 1] < 1) y++; else { d = 1; x++; } break; case 1: if (x < n - 1 && a[x + 1][y] < 1) x++; else { y--; d = 2; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; d = 3; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; d = 0; } break; default: break; } } return a; }  }
Matrix { static int[][] SpiralMatrix(int n) { int[][] a = new int[n][n]; int c = 1, x = 0, y = 0; int d = 0; while (c <= n * n) { if (a[x][y] == 0) { a[x][y] = c++; } else { d++; d = d % 4; } switch (d) { case 0: if (y < n - 1 && a[x][y + 1] < 1) y++; else { d = 1; x++; } break; case 1: if (x < n - 1 && a[x + 1][y] < 1) x++; else { y--; d = 2; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; d = 3; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; d = 0; } break; default: break; } } return a; }  static void main(String[] args); }
Matrix { static int[][] SpiralMatrix(int n) { int[][] a = new int[n][n]; int c = 1, x = 0, y = 0; int d = 0; while (c <= n * n) { if (a[x][y] == 0) { a[x][y] = c++; } else { d++; d = d % 4; } switch (d) { case 0: if (y < n - 1 && a[x][y + 1] < 1) y++; else { d = 1; x++; } break; case 1: if (x < n - 1 && a[x + 1][y] < 1) x++; else { y--; d = 2; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; d = 3; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; d = 0; } break; default: break; } } return a; }  static void main(String[] args);  }