もう1つJavaのolution。
行と列の距離は計算されずに宣言されます。
final int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 1, 2 },
{ 3, 4, 5, 6 } };
final int[][] dRow = { { 1, 0, 0, 0 }, { 1, 1, 0, -1 },
{ 1, 0, -1, -1 }, { 0, 0, 0, -1 } };
final int[][] dCol = { { 0, -1, -1, -1 }, { 0, 0, -1, 0 },
{ 0, 1, 0, 0 }, { 1, 1, 1, 0 } };
int[][] tmp = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
// rotate a to tmp
for (int row = 0; row < a.length; row++)
for (int col = 0; col < a[row].length; col++)
tmp[row][col] = a[row + dRow[row][col]][col + dCol[row][col]];
// copy tmp to a
for (int row = 0; row < a.length; row++)
for (int col = 0; col < a[row].length; col++)
a[row][col] = tmp[row][col];
// show result
for (int row = 0; row < a.length; row++)
for (int col = 0; col < a[row].length; col++)
System.out.print((col == 0 ? "\n" : "") + a[row][col]);
何を試しましたか?スピード、エレガンス、ストレージなどの点で特別な要件/制約がありますか? –
要件はありません。私が書いたように、私はこのインタビューの質問に失敗し、異なる実装を見たり学びたいと思っています。 – AntonIva