2132. Convert 1D Array Into 2D Array
My accepted C++ solution to LeetCode problem 2132, Convert 1D Array Into 2D Array, running in 4ms.
- Difficulty: Easy
- C++
- Runtime 4ms
- Memory 90MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 4ms, memory 90MB, accepted 2025-12-27.
class Solution {
public:
vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {
if (original.size() != m * n) return {};
vector<vector<int>> result(m, vector<int>(n));
for (int i = 0; i < original.size(); i++) {
result[i / n][i % n] = original[i];
}
return result;
}
};