矩陣變形 Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.數組

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.隊列

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.element

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.rem

Example 1:it

Input: 
nums = 
[[1,2],
 [3,4]]
r = 1, c = 4
Output: 
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Example 2:io

Input: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
Output: 
[[1,2],
 [3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Note:function

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

解決方案:class

①須要判斷矩陣是否可以正確的進行轉換(總數目是否相等),其次難點在於數組的轉換,我用了最簡單的直接轉換。用時13ms原理

public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
       int len1 = nums.length;
       int len2 = nums[0].length;
       if(len1 * len2 != r * c){
           return nums;
       }
       int res[][] = new int[r][c];
       int row = 0;
       int col = 0;
       for(int i = 0; i < len1; i ++){
           for(int j = 0; j < len2; j ++){
               res[row][col] = nums[i][j];
               col ++;
               if(col == c){
                   row ++;
                   col = 0;
               }
           }
       }
       return res;
    }
}List

②使用隊列存儲矩陣,此時該矩陣只有一行,更好存儲,用時16 ms

public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int len1 = nums.length;
        int len2 = nums[0].length;
        if (len1 * len2 != r * c) {
            return nums;
        }
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 0;i < len1 ;i ++ ) {
            for (int j = 0;j < len2 ;j ++ ) {
                queue.add(nums[i][j]);
            }
        }
        int res[][] = new int[r][c];
        for (int i = 0;i < r ;i ++ ) {
            for (int j = 0;j < c ;j ++ ) {
                res[i][j] = queue.remove();
            }
        }
        return res;
    }
}

③使用除法和模數,實現比較簡單,可是須要深刻理解數組的原理。咱們想一下二維矩陣存儲在一維空間時的存儲方法爲,nums[i][j]存儲在第nums[i*k+j]個位置,其中k表示矩陣的列數,反之,當咱們由一維回到二維矩陣時,設數組下標爲count,則二維矩陣的顯示爲nums[count/k,count%k],這樣,咱們就能夠獲得對應的矩陣。 用時16ms.

public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int len1 = nums.length;
        int len2 = nums[0].length;
        if (len1 * len2 != r * c) {
            return nums;
        }
        int res[][] = new int[r][c];
        int count = 0;
        for (int i = 0;i < len1 ;i ++ ) {
            for (int j = 0;j < len2 ;j ++ ) {
                res[count / c][count % c] = nums[i][j];
                count ++;
            }
        }
        return res;
    }
}

public class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int len1 = nums.length;//行數
        int len2 = nums[0].length;//列數
        if(len1 * len2 != r * c){
            return nums;
        }
        int res[][] = new int[r][c];
        int count = 0;
        for (int i = 0;i < len1 ;i ++ ) {
            for (int j = 0;j < len2 ;j ++ ) {
                count = i * len2 + j;
                res[count / c][count % c] = nums[i][j];
            }
        }
        return res;
    }
}

以上三種方法均知足時間複雜度:O(m∗n)遍歷了整個矩陣,空間複雜度:O(m∗n),結果矩陣所佔用的空間

相關文章
相關標籤/搜索