9.1数组与字符串(五)——矩阵的转置

/**
 * 功能:给定一幅由N*N矩阵表示的图像,其中每个像素的大小为4字节,编写一个方法,将图像旋转90度,
 * 不占用额外内存空间。
 */

       public static void rotate( int [][] matrix , int n ){
             for ( int layer =0; layer < n /2; layer ++){
                   int first = layer ;
                   int last = n -1- layer ;
                   for ( int i = first ; i < last ; i ++){
                         int top = matrix [ first ][ i ]; //存储上边
                         matrix [ first ][ i ]= matrix [ n -1- i ][ first ]; //左到上
                         matrix [ n -1- i ][ first ]= matrix [ last ][ n -1- i ]; //下到左
                         matrix [ last ][ n -1- i ]= matrix [ i ][ last ]; //右到下
                         matrix [ i ][ last ]= top ; //上到右
                  }
            }
      }
}

你可能感兴趣的:(java,字符串)