LC 867. Transpose Matrix

Nilanjan Deb · April 1, 2020

I solved this problem for my academic course COMPUTER PROGRAMMING. It’s very Simple Problem. Just You Have Traverse through row and fixed a Column at a time. One thing it’s very Important here i.e length of the colum and length of the row found by a simple logic. In my solution You cant got it very simply.

This is my CPP solution.

class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& A) {
        vector<vector<int>>res;
        int col_len = A[0].size();
        int row_len = A.size();
        for(int i=0;i<col_len;i++){
            vector<int>row;
            for(int j=0;j<row_len;j++){
                row.push_back(A[j][i]);
            }
            res.push_back(row);
        }
        return res;
    }
};

Time Complexity O(row_len * col_len); Space Complexity O(row_len * col_len);



Dicussion Forum