-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranspose_matrix.cpp
More file actions
71 lines (66 loc) · 1.69 KB
/
transpose_matrix.cpp
File metadata and controls
71 lines (66 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* =====================================================================================
*
* Filename: transpose_matrix.cpp
*
* Description: 867. Transpose Matrix
*
* Version: 1.0
* Created: 12/10/2023 15:29:04
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::pair;
using std::vector;
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return matrix;
}
vector<vector<int>> trans(matrix[0].size(), vector<int>(matrix.size()));
for (int i = 0; i < trans.size(); i++) {
for (int j = 0; j < trans[i].size(); j++) {
trans[i][j] = matrix[j][i];
}
}
return trans;
}
};
TEST(Solution, transpose) {
vector<pair<vector<vector<int>>, vector<vector<int>>>> cases = {
std::make_pair(
vector<vector<int>>{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
},
vector<vector<int>>{
{1, 4, 7},
{2, 5, 8},
{3, 6, 9},
}),
std::make_pair(
vector<vector<int>>{
{1, 2, 3},
{4, 5, 6},
},
vector<vector<int>>{
{1, 4},
{2, 5},
{3, 6},
}),
};
for (auto& c : cases) {
EXPECT_THAT(Solution().transpose(c.first), testing::ElementsAreArray(c.second));
}
}