一【題目類別】
- 矩陣
二【題目難度】
- 簡(jiǎn)單
三【題目編號(hào)】
- 566.重塑矩陣
四【題目描述】
- 在 MATLAB 中,有一個(gè)非常有用的函數(shù) reshape ,它可以將一個(gè) m x n 矩陣重塑為另一個(gè)大小不同(r x c)的新矩陣,但保留其原始數(shù)據(jù)。
- 給你一個(gè)由二維數(shù)組 mat 表示的 m x n 矩陣,以及兩個(gè)正整數(shù) r 和 c ,分別表示想要的重構(gòu)的矩陣的行數(shù)和列數(shù)。
- 重構(gòu)后的矩陣需要將原始矩陣的所有元素以相同的 行遍歷順序 填充。
- 如果具有給定參數(shù)的 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]]
六【題目提示】
- 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
七【解題思路】
- 例如除法和取模的知識(shí)
- 題目要求按行優(yōu)先重塑矩陣,所以我們遍歷原數(shù)組的元素個(gè)數(shù)(設(shè)置為i),i對(duì)原數(shù)組的列數(shù)做除法就定位到按行優(yōu)先存儲(chǔ)的對(duì)應(yīng)行數(shù),i對(duì)原數(shù)組的列數(shù)取模就定位到按行優(yōu)先存儲(chǔ)時(shí)這一行對(duì)應(yīng)的列數(shù),這樣就可以取出原數(shù)組按行優(yōu)先存儲(chǔ)時(shí)的每一個(gè)元素
- 然后將取出的元素存入創(chuàng)建的擁有新的行和列的新的二維數(shù)組中,在這個(gè)新的二維數(shù)組中,我們?nèi)砸詣偛旁O(shè)置為i去尋找存入位置,i對(duì)新的二維數(shù)組的列數(shù)做除法,就得到了按行優(yōu)先存儲(chǔ)存入的行數(shù),同理,i對(duì)新的二維數(shù)組的列數(shù)取模就定位到按行優(yōu)先存儲(chǔ)時(shí)這一行的列數(shù),這樣就可以將上一步取出來的元素存儲(chǔ)對(duì)應(yīng)位置,實(shí)現(xiàn)了二維數(shù)組的重塑
- 需要注意的是,如果原二維數(shù)組的元素個(gè)數(shù)不等于新的二維數(shù)組的元素的個(gè)數(shù),直接返回原數(shù)組即可,因?yàn)闊o法重塑數(shù)組
- 最后返回結(jié)果即可
- PS:對(duì)于不同語言實(shí)現(xiàn)的細(xì)節(jié)略有不同,具體可見下面的代碼
八【時(shí)間頻度】
- 時(shí)間復(fù)雜度: O ( m ? n ) O(m * n) O(m?n), m 、 n m、n m、n分別為傳入數(shù)組的行數(shù)和列數(shù)
- 空間復(fù)雜度: O ( m ? n ) O(m * n) O(m?n), m 、 n m、n m、n分別為傳入數(shù)組的行數(shù)和列數(shù)
九【代碼實(shí)現(xiàn)】
- 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;
}
}
- 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;
}
- 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
- C++語言版
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;
}
};
十【提交結(jié)果】
-
Java語言版
-
C語言版
-
Python語言版
文章來源:http://www.zghlxwxcb.cn/news/detail-624460.html
-
C++語言版
文章來源地址http://www.zghlxwxcb.cn/news/detail-624460.html
到了這里,關(guān)于【LeetCode每日一題】——566.重塑矩陣的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!