Skip to content

Commit 900ca86

Browse files
committed
Runtime: 35 ms (Top 23.18%) | Memory: 12.2 MB (Top 76.14%)
1 parent d14302c commit 900ca86

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
// Runtime: 35 ms (Top 23.18%) | Memory: 12.2 MB (Top 76.14%)
12
class Solution {
23
public:
34
vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
4-
5+
56
vector<vector<int>> ans;
67
ans.push_back({rStart, cStart}); //Pushing the starting point in answer
78
int topRow = rStart-1; //Row above starting point
89
int bottomRow = rStart+1; //Row below starting point
910
int leftCol = cStart-1; //Col left to starting point
1011
int rightCol = cStart+1; //Col right to starting point
11-
12+
1213
while(topRow != -1 || bottomRow != rows || leftCol != -1 || rightCol != cols){ //Untill all rows and columns are exhausted
13-
14+
1415
if(rightCol != cols){ //Checking if this col is exhausted
1516
for(int i = topRow+1; i < bottomRow;i++) ans.push_back({i, rightCol}); //Running loop from one bottom of top row till bottom row in right col
1617
rightCol++; //Incrementing the col forward
@@ -20,17 +21,17 @@ class Solution {
2021
for(int j = rightCol-1; j > leftCol; j--) ans.push_back({bottomRow, j});
2122
bottomRow++;
2223
}
23-
24+
2425
if(leftCol != -1){
2526
for(int i = bottomRow-1; i > topRow; i--) ans.push_back({i, leftCol});
2627
leftCol--;
2728
}
28-
29+
2930
if(topRow != -1){
3031
for(int j = leftCol+1; j < rightCol; j++) ans.push_back({topRow, j});
3132
topRow--;
32-
}
33+
}
3334
}
3435
return ans;
3536
}
36-
};
37+
};

0 commit comments

Comments
 (0)