반응형

https://leetcode.com/problems/transpose-matrix

 

Transpose Matrix - LeetCode

Can you solve this real interview question? Transpose Matrix - Given a 2D integer array matrix, return the transpose of matrix. The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices. [https://

leetcode.com

 

class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& matrix) {
        int n = matrix.size();
        int m = matrix.front().size();
        vector<vector<int>> res(m, vector<int>(n));

        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                res[j][i] = matrix[i][j];
            }
        }

        return res;
    }
};
반응형

+ Recent posts