1,题目要求
Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix.
求一个矩阵的转置。
2,题目思路
C语言启蒙课的题目,非常简单。
3,程序源码
int x = []() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& A) {
auto m = A.size(), n = A[0].size();
vector<vector<int>> res (n, vector<int>(m, 0));
for(int i = 0;i < m;i++)
for(int j = 0;j < n;j++)
res[j][i] = A[i][j];
return res;
}
};