LeetCode solutions

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

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 4ms, memory 90MB, accepted 2025-12-27.

cpp
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;
    }
};

Source