-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpiralMatrix-IV.cpp
103 lines (86 loc) · 2.92 KB
/
SpiralMatrix-IV.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
#include<bits/stdc++.h>
using namespace std;
class ListNode {
public:
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
void fillSpiral(vector<vector<int> > & matrix, ListNode * &head) {
ListNode* ptr = head;
int r = matrix.size(), c = matrix[0].size();
int startCol = 0, startRow = 0, endCol = c - 1, endRow = r - 1;
int cnt = 0;
while(cnt < r*c) {
for(int i = startCol; i <= endCol && cnt < r*c; i++){
if(ptr) {
matrix[startCol][i] = ptr->val;
ptr = ptr->next;
}
else matrix[startCol][i] = -1;
cnt++;
}
startRow++;
for(int i = startRow; i<= endRow && cnt < r*c; i++){
if(ptr){
matrix[i][endCol] = ptr->val;
ptr = ptr->next;
} else matrix[i][endCol] = -1;
cnt++;
}
endCol--;
for(int i = endCol; i >= startCol && cnt < r*c; i--){
if(ptr){
matrix[endRow][i] = ptr->val;
ptr = ptr->next;
} else matrix[endRow][i] = -1;
cnt++;
}
endRow--;
for(int i = endRow; i >= startRow && cnt < r*c; i--){
if(ptr){
matrix[i][startCol] = ptr->val;
ptr = ptr->next;
} else matrix[i][startCol] = -1;
cnt++;
}
startCol++;
}
}
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
// * Create an m*n matrix
vector<vector<int>> spiral(m, vector<int> (n, 0));
// * Fill spiral matrix with linkedlist data
fillSpiral(spiral, head);
return spiral;
}
// * MUCH BETTER CODE (After removing unneccessary stuff)
void fillSpiral(vector<vector<int> > & matrix, ListNode * &head) {
int r = matrix.size(), c = matrix[0].size();
int startCol = 0, startRow = 0, endCol = c - 1, endRow = r - 1;
// int cnt = 0; // * This can be avoided because linkedlist nodes are in range [1, m*n]
// * As soon as nodes are not left, stop the LOOP!!
while(head) {
for(int i = startCol; i <= endCol && head; i++, head = head->next)
matrix[startCol][i] = head->val;
startRow++;
for(int i = startRow; i <= endRow && head; i++, head = head->next)
matrix[i][endCol] = head->val;
endCol--;
for(int i = endCol; i >= startCol && head; i--, head = head->next)
matrix[endRow][i] = head->val;
endRow--;
for(int i = endRow; i >= startRow && head; i--, head = head->next)
matrix[i][startCol] = head->val;
startCol++;
}
}
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
// * Create an m*n matrix, and initially fill with -1
vector<vector<int>> spiral(m, vector<int> (n, -1));
// * Fill spiral matrix with linkedlist data
fillSpiral(spiral, head);
return spiral;
}