2016-03-07 174 views
0
public int[][] Solution(int[][] matrix, int flag) { 
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return matrix; 
    //int m = matrix.length, n = matrix[0].length; 
    int[][] rvalue; 
    rvalue = transpose(matrix); 
    flip(rvalue, flag); 
    return rvalue; 
} 
// transporse the matrix 
private int[][] transpose(int[][] matrix) { 
    int m = matrix.length, n = matrix[0].length; 
    int[][] rvalue = new int[n][m]; 
    for (int i = 0; i < n; i++) 
     for (int j = 0; j < m; j++) 
      rvalue[i][j] = matrix[j][i]; 
    return rvalue; 
} 
// clockwise rotate the matrix 
private void flip(int[][] matrix, int flag) { 
    int m = matrix.length, n = matrix[0].length; 
    if (flag == 1) {  
     for (int i = 0; i < m; i++) 
      for (int j = 0; j < n/2; j++) { 
       matrix[i][j] ^= matrix[i][n-j-1]; // line 1 
       matrix[i][n-j-1] ^= matrix[i][j]; // line 2 
       matrix[i][j] ^= matrix[i][n-j-1]; // line 3 
      } 
    } 
} 

上面是用於旋轉矩陣(第一次transporse,然後旋轉)的代碼。但我無法理解第1,2和3行的代碼,我用我自己的下面的代碼替換了這三行代碼,它運行良好。在Java中旋轉矩陣混淆

int temp=matrix[i][j]; 
matrix[i][j]=matrix[i][matrix[0].length-j-1]; 
matrix[i][matrix[0].length-j-1]=temp; 

有人可以解釋原來的三線在做什麼?

+0

哪一行到底?曲子在你發佈的代碼線1是'公共INT [] []溶液(INT [] []矩陣,INT標誌){' – Guiroux

回答