add: 220602 [cpp]

This commit is contained in:
Eat-Swap 2022-06-02 11:17:34 +08:00
parent 61780faad2
commit 7c522bb588
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 20 additions and 1 deletions

19
cpp/2206/220602.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <vector>
/**
* 867. 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.
*/
class Solution {
public:
static std::vector<std::vector<int>> transpose(std::vector<std::vector<int>>& matrix) {
const int m = matrix.size(), n = matrix.front().size();
std::vector<std::vector<int>> ret(n, std::vector<int>(m));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ret[j][i] = matrix[i][j];
return ret;
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2206)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2206 220601-CN.cpp)
ADD_EXECUTABLE(2206 220602.cpp)