長期更新的典型面試題庫,附帶完整可運行代碼與輸出結果。
思路: 列變行(正序),行變列(逆序)。即 b[j][n-1-i] = a[i][j]。
public class Rotate {
public static void getRotate(int[][] a) {
int[][] b = new int[a[0].length][a.length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
b[j][a.length - 1 - i] = a[i][j];
// 輸出
for (int[] row : b) {
for (int v : row) System.out.print(v + " ");
System.out.println();
}
}
public static void main(String[] args) {
int[][] a = {{0,1,2},{0,1,5},{1,1,6},{1,2,5},{1,2,5}};
getRotate(a);
}
}
運行結果:
1 1 1 0 0 2 2 1 1 1 5 5 6 5 2
public class Job8 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + "*" + i + "=" + (i*j) + "\t");
System.out.println();
}
}
}
運行結果:
