2022新年快乐!新年第一题!(Leetcode 2022.将一维数组转变成二维数组)
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& p, int n, int m) {
vector<vector<int>> ans(n, vector<int>(m));
if(n * m != p.size()) return {};
int x = 0, y = 0;
for(auto num: p)
{
ans[x][y] = num;
y = y + 1;
if(y >= m)
{
y = 0, x ++;
}
}
return ans;
}
};