How to improve ? C++


  • 0
    Y
    class Solution {
    public:
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
            int row, col;
            int m = nums.size(), n = nums[0].size();
            vector<int> vec;
            if (m * n != r * c){
                return nums;
            }
            vector<vector<int>> result(r, vector<int>(c, 0));
            int index = 0;
            for (int i=0; i<r; i++){
                for (int j=0; j<c; j++){
                    index = c * i + j;
                    row = index / n ;
                    col = (index-n*row) % n;
                    result[i][j] = nums[row][col];
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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