[LeetCode] July LeetCoding Challenge 2021 - Week 1 Reshape the Matrix (6th, July)
Reshape the Matrix
📃 Problem
https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3803/
💡 Check Point
r, c
are possible and legal. 👩💻 My Solution
class Solution {
public:
vector<vector<int> > matrixReshape(vector<vector<int> >& mat, int r, int c) {
vector<vector<int> > result;
// Define original matrix's row and column
// m = original row, n = original colum
int m = mat.size();
int n = mat[0].size();
// If we could not create a reshaped matrix with using given values c, r
// => return an origianl matrix
if(r*c != m*n) {
return mat;
}
// Otherwise, create a reshaped matrix!
for(int i = 0; i < r; i++) {
vector<int> values;
for(int j = 0; j < c; j++) {
// Define k which calculate a new matrix's 1d index
int k = c * i + j;
// Calculate 2d indices using 1d index
values.push_back(mat[k/n][k%n]);
}
result.push_back(values);
}
return result;
}
};
Reference
この問題について([LeetCode] July LeetCoding Challenge 2021 - Week 1 Reshape the Matrix (6th, July)), 我々は、より多くの情報をここで見つけました https://velog.io/@yeahsilver/LeetCode-July-LeetCoding-Challenge-2021-Week-1-Reshape-the-Matrix-6th-Julyテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol