Skip to content

Commit 8935f2d

Browse files
committed
566. Reshape the Matrix
1 parent da78674 commit 8935f2d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

reshape-the-matrix.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//Runtime: 43 ms
2+
class Solution {
3+
public:
4+
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
5+
vector<vector<int>>res;
6+
int m = nums.size();
7+
if (m == 0)
8+
return nums;
9+
int n = nums[0].size();
10+
11+
if (m * n != r * c)
12+
return nums;
13+
14+
vector<int>t;
15+
for (int i = 0; i < m; i++)
16+
for (int j = 0; j < n; j++)
17+
{
18+
t.push_back(nums[i][j]);
19+
if (t.size() == c)
20+
{
21+
res.push_back(t);
22+
t.clear();
23+
}
24+
}
25+
return res;
26+
}
27+
};

0 commit comments

Comments
 (0)