先交换X!=Y的坐标,如果顺时针转,每个Row reverse。如果逆时针,每个Col reverse。

public int[][] Rotate2dArray(int[][] Matrix, int flag){
   int Rl = Matrix.length;
   int Cl = Matrix[0].length;
   int[][] res = new int[Cl][Rl];
   for(int i =0;i<Cl;i++){
     for(int j=0; j<Rl; j++){
     res[i][j] = Matrix[j][i];
  }
}
  if(flag==1){
//顺时针
     for(int i =0;i<Cl;i++){
       for(int j=0; j<Rl/2; j++){
         int tmp = res[i][j];
         res[i][j] = res[i][Rl-1-j];
         res[i][Rl-1-j] = tmp;
       }
     }
    }
   else{
     for(int i =0;i<Cl/2;i++){
      for(int j=0; j<Rl; j++){
        int tmp = res[i][j];
        res[i][j] = res[Cl-i-1][j];
        res[Cl-i-1][j] = tmp;
        }
      }
}
   return res;
}



  public static void main(String[] args) {
   Solution test = new Solution();
   int[][] multi2 = new int[2][2];

    int[][] multi = new int[][]{
            { 5,6 },
            { 8,9 },
          };

     multi2 =  test.Rotate2dArray(multi,0);

      System.out.println(Arrays.deepToString(multi2));

    }

results matching ""

    No results matching ""