-
Notifications
You must be signed in to change notification settings - Fork 0
/
566.hpp
62 lines (49 loc) · 1.46 KB
/
566.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef LEETCODE_566_HPP
#define LEETCODE_566_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
using namespace std;
/**
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.
*/
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>> &nums, int r, int c) {
auto m = nums.size();
if (m == 0) {
return nums;
}
auto n = nums[0].size();
if (m == r || m * n != r * c) {
return nums;
}
vector<vector<int>> res((unsigned long) r, vector<int>((unsigned long) c));
int pi = 0, pj = 0;
for (auto row : nums) {
for (auto v : row) {
res[pi][pj++] = v;
if (pj == c) {
pj = 0;
pi++;
}
if (pi == r) {
return res;
}
}
}
return res;
}
};
#endif //LEETCODE_566_HPP