1、对于C++,vector<vector<int>>
(1)、赋值:首先处理内层容器,再push_back到外层容器中;
(2)、单纯声明之后为空,需要resize()再使用,内层Obj[0].resize(),外层直接用;
(3)、vector的赋值;
(4)、像二维数组一样访问;
(5)、相关函数。
2、C 不知所云
3、转置前后,m*n -> n*m
//C++
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& A) {
unsigned int m = A.size();
unsigned int n = A[0].size();
vector<vector<int>> Matrix(n);
for(int i = 0; i < n; i++){
Matrix[i].resize(m);
for(int j = 0; j <m; j++){
Matrix[i][j] = A[j][i];
}
}
return Matrix;
}
};
或者
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& A) {
unsigned int m = A.size();
unsigned int n = A[0].size();
vector<vector<int>> Matrix(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
Matrix[i].push_back(A[j][i]);
}
}
return Matrix;
}
};