矩阵变形 Reshape the Matrix

本文探讨了在给定条件下如何将一个二维数组重塑成不同大小的新矩阵,同时保持原有的数据不变。提供了三种不同的实现方法,并详细解释了每种方法的原理及优劣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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

Example 1:

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:

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:

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

解决方案:

①需要判断矩阵是否能够正确的进行转换(总数目是否相等),其次难点在于数组的转换,我用了最简单的直接转换。用时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;
    }
}

②使用队列存储矩阵,此时该矩阵只有一行,更好存储,用时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),结果矩阵所占用的空间

转载于:https://my.oschina.net/liyurong/blog/900654

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值