-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1329_Sort_the_Matrix_Diagonally.cpp
More file actions
66 lines (56 loc) · 1.89 KB
/
1329_Sort_the_Matrix_Diagonally.cpp
File metadata and controls
66 lines (56 loc) · 1.89 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
class Solution {
public:
vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
int row = mat.size();
int col = mat[0].size();
//vector<vector<int>> result(row, vector<int> (col));
int temp_row;
int temp_col;
vector<int> temp;
for(int i = 0; i < col; ++i) {
temp_row = 0;
temp_col = i;
//vector<int> temp{};
temp = {};
while(temp_row >= 0 && temp_row < row && temp_col >= 0 && temp_col < col) {
temp.push_back(mat[temp_row][temp_col]);
temp_row++;
temp_col++;
}
sort(temp.begin(), temp.end());
temp_row = 0;
temp_col = i;
int j = 0;
while(temp_row >= 0 && temp_row < row && temp_col >=0 && temp_col < col) {
mat[temp_row][temp_col] = temp[j];
j++;
temp_row++;
temp_col++;
}
}
for(int i = 1; i < row; ++i) {
temp_row = i;
temp_col = 0;
//vector<int> temp{};
temp = {};
while(temp_row >= 0 && temp_row < row && temp_col >= 0 && temp_col < col) {
temp.push_back(mat[temp_row][temp_col]);
temp_row++;
temp_col++;
}
sort(temp.begin(), temp.end());
temp_row = i;
temp_col = 0;
int j = 0;
//cout<<temp.size();
while(temp_row >= 0 && temp_row < row && temp_col >=0 && temp_col < col) {
//cout<<"hi";
mat[temp_row][temp_col] = temp[j];
j++;
temp_row++;
temp_col++;
}
}
return mat;
}
};