2014-02-24 24 views
0

問題給出2D array寫一個方法來反轉列。如果可能的話,做它in-place 我已經實施,它工作正常,但它不是in-place。它使用auxillary storage,是否有可能反轉2D矩陣的列而不使用矩陣。方法在Java中執行二維數組中列的反轉操作

Here is my code: 
public static int[][] reverseColumns(int[][] matrix){ 
     int rows=matrix.length; 
     int cols=matrix[0].length; 
     int temp=0; 

     int[][] result= new int[rows][cols]; 

     for (int row=rows-1; row >=0; row--){ 
      for (int col=0;col<cols;col++){ 
       result[row][col]=matrix[temp][col]; 
      } 
      temp++; 
     } 
     return result; 
    } 

    public static void print2DArray(int[][] result){ 
    for (int i=0; i < result.length;i++){ 
     System.out.println(Arrays.toString(result[i])); 

    } 
} 

public static void main(String[] args) 
    { 
     int[][] matrix = { 
       {1,2,3,4}, 
       {5,6,7,8}, 
       {9,10,11,12}, 
       {13,14,15,16} 
      int[][] result = reverseColumns(matrix); 
    print2DArray(result);  
    System.out.println() 
     }; 

輸出是:

[13, 14, 15, 16] 
[9, 10, 11, 12] 
[5, 6, 7, 8] 
[1, 2, 3, 4] 
+0

你爲什麼要這麼做?是否有性能問題?如果可以接受,則可以打印反轉的列 – Terraego

回答

2

我按照cols和rows逆轉的語義爲你的建議:

1 2 3          7 8 9 
    4 5 6 _______column reversal_______ 4 5 6 (vertically reversed) 
    7 8 9          1 2 3 



    1 2 3          3 2 1 
    4 5 6 _______row reversal__________ 6 5 4 (horizontally reversed) 
    7 8 9          9 8 7 

有可能爲horizontal (row reversal)in-place: 這很簡單。對於vertical (col reversal),它需要更多的理解。這裏的方法是;舉一個例子matrix並嘗試遵循的步驟,你就會明白

public static void reverseColumnsInPlace(int[][] matrix){ 
     for(int col = 0;col < matrix[0].length; col++){ 
      for(int row = 0; row < matrix.length/2; row++) { 
       int temp = matrix[row][col]; 
       matrix[row][col] = matrix[matrix.length - row - 1][col]; 
       matrix[matrix.length - row - 1][col] = temp; 
      } 
    } 
} 

public static void reverseRowsInPlace(int[][] matrix){ 

    for(int row = 0; row < matrix.length; row++){ 
     for(int col = 0; col < matrix[row].length/2; col++) { 
      int temp = matrix[row][col]; 
      matrix[row][col] = matrix[row][matrix[row].length - col - 1]; 
      matrix[row][matrix[row].length - col - 1] = temp; 
     } 
    } 
} 
0

訣竅是具有計數器變量,說row

然後循環只到陣列的中間,並做

tmp = matrix[row]; 
matrix[row]=matrix[rows-1-row]; 
matrix[rows-1-row]=tmp; 

其中rows是總數。

我會讓你找出tmp的類型。

因爲tmp只是一個參考(Java中的4個字節),所以這隻使用一個常量輔助存儲,

public static int[][] reverseColumns(int[][] matrix){ 
    int rows=matrix.length; 
    int[] temp=null; 
    for (int row=rows-1; row>=rows/2; row--){ 
     temp = matrix[row]; 
     matrix[row]=matrix[rows-1-row]; 
     matrix[rows-1-row] = temp; 
    } 
    return matrix; 
} 
+0

我的代碼反轉了數組的列。請再次檢查 – eagertoLearn

+0

要顛倒這些列意味着最後一列變成第一,倒數第二變成第二,等等。至少我是這麼理解的。你的代碼反轉了每列中的元素,這意味着它顛倒了行。檢查你的輸入/輸出。 –

+0

我想象它是每列垂直向下運行,所以反向列意味着顛倒這條veritcal線,這就是我的代碼所做的。但是你的建議告訴我,顛倒數組列表中的每個數組都是顛倒了列,我想這是行... – eagertoLearn

-1

這可以幫助你與就地交流:

private void swap(int[][] A, int row1, int col1, int row2, int col2) { 
    int tmp = A[row1][col1]; 
    A[row1][col1] = A[row2][col2]; 
    A[row2][col2] = tmp; 
}