0

Is there a way to find transpose a 2D vector matrix without allocating another 2D vector??

Sample Testcase

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]

Code that I tried

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int i,j, n=matrix.size(),temp;
        for(i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                temp=matrix[i][j];
                matrix[i][j]=matrix[j][i];
                matrix[j][i]=temp;
            }
        }
        for(i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }
    }
};

Printed Output:

1 2 3 
4 5 6 
7 8 9 

Expected Output:

1 4 7
2 5 9
3 6 9 

Edited:

New Code:

class Solution {
public:
    void transpose(vector<vector<int>>& mat, int n) {
        int i,j,temp;
        for(i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                temp=mat[i][j];
                mat[i][j]=mat[j][i];
                mat[j][i]=temp;
            }
        }
    }
    void rotate(vector<vector<int>>& matrix) {
        int n=matrix.size(),i,j;
        transpose(matrix, n);
        for(i=0; i<n; i++) {
            for(j=0; j<n; j++) {
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
        }
    }
};

Thanks in advance

9
  • 1
    Yes, just swap elements Commented Jan 5, 2022 at 8:30
  • Nope it does work the same way as 2D array matrix Commented Jan 5, 2022 at 8:32
  • 3
    No @paddy is right, swap the elements you need to move around e.g. std::swap(a[0][2],a[2][0]). Commented Jan 5, 2022 at 8:34
  • 1
    No it will be done in place. By passing the matrix by reference to rotate you don't make a copy Commented Jan 5, 2022 at 8:36
  • 2
    You're swapping every possible combination. You should only traverse a single triangle either side of the diagonal. e.g. for (j = 0; j < i; j++) <-- note j < i not j < n. It would be very straight forward for you to work through this by hand on paper and work out how your swaps should go. Also, use std::swap instead of the 3-line version you're doing. Commented Jan 5, 2022 at 8:47

1 Answer 1

1

Below is the code for inplace(Fixed space) || n*n matrix

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int i,j,n=matrix.size();
        for(i=0; i<n; i++) {
            // Instead of j starting with 0 every time, its needs to start from i+1
            for(j=i+1; j<n; j++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
        for(int i=0; i<n;i++) {
            reverse(matrix[i].begin(), matrix[i].end());
        }
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Note that there's no point in starting the j-loop at i because that is on the diagonal. Either use i+1 or use the loop I suggested in the question comments.
that's a valid point

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.