【LeetCode每日一题】——566.重塑矩阵

文章目录

一【题目类别】

  • 矩阵

二【题目难度】

  • 简单

三【题目编号】

  • 566.重塑矩阵

四【题目描述】

  • 在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。
  • 给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
  • 重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
  • 如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。

五【题目示例】

  • 示例 1:

    • 输入:mat = [[1,2],[3,4]], r = 1, c = 4
    • 输出:[[1,2,3,4]]
  • 示例 2:

    • 输入:mat = [[1,2],[3,4]], r = 2, c = 4
    • 输出:[[1,2],[3,4]]

六【题目提示】

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> m = = m a t . l e n g t h m == mat.length </math>m==mat.length
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> n = = m a t [ i ] . l e n g t h n == mat[i].length </math>n==mat[i].length
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = m , n < = 100 1 <= m, n <= 100 </math>1<=m,n<=100
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> − 1000 < = m a t [ i ] [ j ] < = 1000 -1000 <= mat[i][j] <= 1000 </math>−1000<=mat[i][j]<=1000
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = r , c < = 300 1 <= r, c <= 300 </math>1<=r,c<=300

七【解题思路】

  • 例如除法和取模的知识
  • 题目要求按行优先重塑矩阵,所以我们遍历原数组的元素个数(设置为i),i对原数组的列数做除法就定位到按行优先存储的对应行数,i对原数组的列数取模就定位到按行优先存储时这一行对应的列数,这样就可以取出原数组按行优先存储时的每一个元素
  • 然后将取出的元素存入创建的拥有新的行和列的新的二维数组中,在这个新的二维数组中,我们仍以刚才设置为i去寻找存入位置,i对新的二维数组的列数做除法,就得到了按行优先存储存入的行数,同理,i对新的二维数组的列数取模就定位到按行优先存储时这一行的列数,这样就可以将上一步取出来的元素存储对应位置,实现了二维数组的重塑
  • 需要注意的是,如果原二维数组的元素个数不等于新的二维数组的元素的个数,直接返回原数组即可,因为无法重塑数组
  • 最后返回结果即可
  • PS:对于不同语言实现的细节略有不同,具体可见下面的代码

八【时间频度】

  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( m ∗ n ) O(m * n) </math>O(m∗n), <math xmlns="http://www.w3.org/1998/Math/MathML"> m 、 n m、n </math>m、n为分别为传入数组的行数和列数
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( m ∗ n ) O(m * n) </math>O(m∗n), <math xmlns="http://www.w3.org/1998/Math/MathML"> m 、 n m、n </math>m、n为分别为传入数组的行数和列数

九【代码实现】

  1. Java语言版
java 复制代码
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语言版
c 复制代码
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语言版
python 复制代码
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++语言版
cpp 复制代码
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;
    }
};

十【提交结果】

  1. Java语言版

  2. C语言版

  3. Python语言版

  4. C++语言版

相关推荐
xier_ran14 小时前
Transformer:Decoder 中,Cross-Attention 所用的 K(Key)和 V(Value)矩阵,是如何从 Encoder 得到的
深度学习·矩阵·transformer
gihigo199815 小时前
MATLAB使用遗传算法解决车间资源分配动态调度问题
算法·matlab
墨染点香15 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
却道天凉_好个秋15 小时前
目标检测算法与原理(一):迁移学习
算法·目标检测·迁移学习
西西弗Sisyphus16 小时前
线性代数 - LU分解(LU-Factorization、LU Decomposition)
线性代数·矩阵·矩阵分解
兮山与16 小时前
算法24.0
算法
晓北斗NorSnow16 小时前
机器学习核心算法与学习资源解析
学习·算法·机器学习
hans汉斯17 小时前
【计算机科学与应用】基于BERT与DeepSeek大模型的智能舆论监控系统设计
大数据·人工智能·深度学习·算法·自然语言处理·bert·去噪
多喝开水少熬夜18 小时前
损失函数系列:focal-Dice-vgg
图像处理·python·算法·大模型·llm
立志成为大牛的小牛18 小时前
数据结构——三十七、关键路径(王道408)
数据结构·笔记·程序人生·考研·算法