[LeetCode Daily Question] - 566. Reshape the Matrix

One [topic category]

  • matrix

Two [question difficulty]

  • Simple

Three [topic number]

  • 566. Reshape Matrix

Four [title description]

  • In MATLAB, there is a very useful function reshape, which can reshape an mxn matrix into another new matrix of different size (rxc), but keep its original data.
  • You are given an mxn matrix represented by a two-dimensional array mat, and two positive integers r and c, representing the number of rows and columns of the desired reconstructed matrix, respectively.
  • The reconstructed matrix needs to fill all the elements of the original matrix in the same row traversal order.
  • If a reshape operation with the given parameters is feasible and reasonable, output the new reshape matrix; otherwise, output the original matrix.

Five [topic examples]

  • Example 1:

    • insert image description here
    • Input: mat = [[1,2],[3,4]], r = 1, c = 4
    • Output: [[1,2,3,4]]
  • Example 2:

    • insert image description here
    • Input: mat = [[1,2],[3,4]], r = 2, c = 4
    • Output: [[1,2],[3,4]]

Six [topic prompt]

  • m = = m a t . l e n g t h m == mat.length m==mat.length
  • n = = m a t [ i ] . l e n g t h n == mat[i].length n==mat[i].length
  • 1 < = m , n < = 100 1 <= m, n <= 100 1<=m,n<=100
  • − 1000 < = m a t [ i ] [ j ] < = 1000 -1000 <= mat[i][j] <= 1000 1000<=mat[i][j]<=1000
  • 1 < = r , c < = 300 1 <= r, c <= 300 1<=r,c<=300

Seven [problem-solving ideas]

  • Knowledge such as division and modulus
  • The title requires to reshape the matrix by row priority, so we traverse the number of elements of the original array (set to i), and i divides the number of columns of the original array to locate the corresponding row number stored by row priority. The modulus of the number of columns is located at the number of columns corresponding to the row when the row is stored first, so that each element of the original array can be taken out when the row is first stored
  • Then store the extracted elements into a new two-dimensional array with new rows and columns. In this new two-dimensional array, we still use i to find the storage position just now. Divide the number of columns of the two-dimensional array to get the number of rows stored in the row-first storage. Similarly, i takes the modulus of the number of columns of the new two-dimensional array to locate the number of columns in this row when the row is first stored. In this way, the elements taken out in the previous step can be stored in the corresponding position, realizing the reshaping of the two-dimensional array
  • It should be noted that if the number of elements in the original two-dimensional array is not equal to the number of elements in the new two-dimensional array, just return the original array directly, because the array cannot be reshaped
  • Finally return the result
  • PS: The details of different language implementations are slightly different, see the following code for details

Eight 【Time Frequency】

  • Time complexity: O ( m ∗ n ) O(m * n)O(mn) m 、 n m、n m and n are the number of rows and columns of the incoming array respectively
  • Space complexity: O ( m ∗ n ) O(m * n)O(mn) m 、 n m、n m and n are the number of rows and columns of the incoming array respectively

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public int[][] matrixReshape(int[][] mat, int r, int c) {
    
    
        int m = mat.length;
        int n = mat[0].length;
        if(m * n != r * c){
    
    
            return mat;
        } 
        int[][] res = new int[r][c];
        for(int i = 0;i < m * n;i++){
    
    
            res[i / c][i % c] = mat[i / n][i % n];
        }
        return res;
    }
}
  1. C language version
int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes)
{
    
    
    int m = matSize;
    int n = matColSize[0];
    if(m * n != r * c)
    {
    
    
        *returnSize = matSize;
        *returnColumnSizes = matColSize;
        return mat;
    }
    *returnSize = r;
    *returnColumnSizes = (int*)malloc(sizeof(int) * r);
    int** res = (int**)malloc(sizeof(int*) * r);
    for(int i = 0;i < r;i++)
    {
    
    
        (*returnColumnSizes)[i] = c;
        res[i] = (int*)malloc(sizeof(int) * c);
    }
    for(int i = 0;i < m * n;i++)
    {
    
    
        res[i / c][i % c] = mat[i / n][i % n];
    }
    return res;
}
  1. Python language version
class Solution:
    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
        m = len(mat)
        n = len(mat[0])
        if m * n != r * c:
            return mat
        res = [[0] * c for _ in range(r)]
        for i in range(0,m * n):
            res[i // c][i % c] = mat[i // n][i % n]
        return res
  1. C++ language version
class Solution {
    
    
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
    
    
        int m = mat.size();
        int n = mat[0].size();
        if(m * n != r * c){
    
    
            return mat;
        }
        vector<vector<int>> res(r,vector<int>(c));
        for(int i = 0;i < m * n;i++){
    
    
            res[i / c][i % c] = mat[i / n][i % n];
        }
        return res;
    }
};

Ten【Submission Results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/132006173