classSolution { publicint[][] generateMatrix(int n) { intl=0, r = n - 1, t = 0, b = n - 1; int[][] mat = newint[n][n]; intnum=1, tar = n * n; while(num <= tar){ for(inti= l; i <= r; i++) mat[t][i] = num++; // left to right. t++; for(inti= t; i <= b; i++) mat[i][r] = num++; // top to bottom. r--; for(inti= r; i >= l; i--) mat[b][i] = num++; // right to left. b--; for(inti= b; i >= t; i--) mat[i][l] = num++; // bottom to top. l++; } return mat; } }