From 7c522bb58892db28bc6bf622e0027d1a67285763 Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Thu, 2 Jun 2022 11:17:34 +0800 Subject: [PATCH] add: 220602 [cpp] --- cpp/2206/220602.cpp | 19 +++++++++++++++++++ cpp/2206/CMakeLists.txt | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 cpp/2206/220602.cpp diff --git a/cpp/2206/220602.cpp b/cpp/2206/220602.cpp new file mode 100644 index 0000000..e556957 --- /dev/null +++ b/cpp/2206/220602.cpp @@ -0,0 +1,19 @@ +#include + +/** + * 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> transpose(std::vector>& matrix) { + const int m = matrix.size(), n = matrix.front().size(); + std::vector> ret(n, std::vector(m)); + for (int i = 0; i < m; ++i) + for (int j = 0; j < n; ++j) + ret[j][i] = matrix[i][j]; + return ret; + } +}; diff --git a/cpp/2206/CMakeLists.txt b/cpp/2206/CMakeLists.txt index 128c9a8..fc3a086 100644 --- a/cpp/2206/CMakeLists.txt +++ b/cpp/2206/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2206) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2206 220601-CN.cpp) +ADD_EXECUTABLE(2206 220602.cpp)