-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path867.cpp
115 lines (102 loc) · 2.67 KB
/
867.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int r=matrix.size();
if (r){
int c=matrix[0].size();
vector<vector<int>> Answer(c,vector<int>(r));
for(int j=0;j<r;j++){
for(int i=0;i<c;i++)
Answer[i][j]=matrix[j][i];
}
return Answer;
}
else{
return vector<vector<int>>();
}
}
};
template <int... Ints>
using int_sequence = integer_sequence<int, Ints...>;
template <int I, int... N>
constexpr auto cat(int_sequence<N...>) { return int_sequence<I, N...>(); }
template <int I, int... N>
void print(int_sequence<I, N...>)
{
cout << I << " ";
if constexpr (sizeof...(N) == 0)
cout << endl;
else
print(int_sequence<N...>());
}
template <int Itop, int... I>
constexpr int gethead(int_sequence<Itop, I...>) { return Itop; }
template <int index, int Itop, int... I>
constexpr int getvalue(int_sequence<Itop, I...>)
{
if constexpr (index == 0)
return Itop;
else
return getvalue<index - 1>(int_sequence<I...>());
}
template <int row, int columns, int... I>
struct Matrix
{
public:
static const int r = row;
static const int c = columns;
template <int i, int j>
static const int value = getvalue<i * columns + j>(int_sequence<I...>());
};
template <int index = 0, int... I>
void printN(Matrix<I...> _)
{
if constexpr (index >= _.r * _.c)
return;
else
{
cout << getvalue<index + 2>(int_sequence<I...>()) << " ";
if constexpr ((index + 1) % _.c == 0)
cout << endl;
printN<index + 1>(_);
}
}
template<int row,int columns, int ...I>
auto make_matrix(int_sequence<I...>){
return Matrix<row,columns,I...>();
}
template <int index = 0, int... I>
auto transpose2list(Matrix<I...> _)
{
if constexpr (index >= _.r * _.c)
return int_sequence<>();
else
{
constexpr int number=getvalue<(index/_.c)+(index%_.c)*_.c+2>(int_sequence<I...>());
return cat<number>(transpose2list<index+1>(_));
}
}
template<int... I>
auto transpose(Matrix<I...> _){
return make_matrix<_.r,_.c>(transpose2list(_));
}
int main(){
Solution s;
vector<vector<int>> test={{1,0,0},{0,1,0},{0,1,1}};
auto result=s.transpose(test);
for(auto i:result){
for (auto j:i)
cout<<j<<" ";
cout<<endl;
}
cout<<endl;
auto template_test=Matrix<3,3,1,0,0,0,1,0,0,1,1>();
printN(template_test);
cout<<endl;
auto template_result=transpose(template_test);
printN(template_result);
return 1;
}