C++ solution


  • 0
    G

    class Solution
    {
    public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c)
    {
    vector<vector<int>>temp;
    vector<int>temp1;
    vector<int>temp2;
    int count=0;
    int cnt = 0;
    for (auto &i : nums)
    {
    for (auto &j : i)
    {
    ++count;
    temp1.push_back(j);
    }
    }
    if (count != r*c)
    return nums;
    else
    {
    for (int i = 0; i < r; ++i)
    {
    for (int j = 0; j < c; ++j)
    {
    temp2.push_back(temp1[cnt]);
    ++cnt;
    }

    		temp.push_back(temp2);
             temp2.erase(temp2.begin(),temp2.end());
    	}
    	return temp;
    }
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.