面试题答案
一键面试#include <iostream>
#include <vector>
class Matrix {
private:
int rows;
int cols;
std::vector<std::vector<int>> data;
public:
Matrix(int r, int c) : rows(r), cols(c), data(r, std::vector<int>(c)) {}
Matrix transpose() {
Matrix transposed(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
transposed.data[j][i] = data[i][j];
}
}
return transposed;
}
};