https://i.imgur.com/dVRdutZ.png
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.71.204 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725160758.A.036.html
2022. Convert 1D Array Into 2D Array
這次算簡單,
但是要判斷無法建立 2D array 的要求滿多餘的。
練習用 iterator 寫寫看
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& original, int m, int n)
{
if (original.size() != m * n) {
return vector<vector<int>>();
}
vector<vector<int>> result;
for (int i = 0; i < m; i++) {
int startIndex = i * n;
int endInex = (i + 1) * n; // exclusive index
vector<int> subArray(next(original.cbegin(), startIndex),
next(original.cbegin(), endInex));
result.emplace_back(subArray);
}
return result;
}
};
--