Skip to content

Commit e9bd9bf

Browse files
author
Tushar Borole
committed
54. Spiral Matrix
1 parent 843d1e6 commit e9bd9bf

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

.idea/workspace.xml

Lines changed: 23 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) |
101101
| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | |
102102
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Dynamic Programming](max_product.js) | 60 ms | 35 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=-rUBh45rugs) |
103+
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [spiral_matrix.js](spiral_matrix.js) | 56 ms | 34 MB | Medium | | | [:link:](https://www.youtube.com/watch?v=3joo9yAZVh8&t=187s) |
103104
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [Decode String](decode_string.js) | 68 ms | 33.7 MB | Medium | | | |
104105
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) |
105106
| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) |

spiral_matrix.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number[]}
4+
*/
5+
var spiralOrder = function(matrix) {
6+
if (matrix.length === 0) return [];
7+
let rowBegin = 0;
8+
let rowEnd = matrix.length - 1;
9+
let columnBegin = 0;
10+
let columnEnd = matrix[0].length - 1;
11+
const res = [];
12+
13+
while (rowBegin <= rowEnd && columnBegin <= columnEnd) {
14+
for (let i = columnBegin; i <= columnEnd; i++) {
15+
res.push(matrix[rowBegin][i]);
16+
}
17+
rowBegin++;
18+
19+
for (let i = rowBegin; i <= rowEnd; i++) {
20+
res.push(matrix[i][columnEnd]);
21+
}
22+
columnEnd--;
23+
24+
if (rowBegin <= rowEnd) {
25+
for (let i = columnEnd; i >= columnBegin; i--) {
26+
res.push(matrix[rowEnd][i]);
27+
}
28+
}
29+
rowEnd--;
30+
31+
if (columnBegin <= columnEnd) {
32+
for (let i = rowEnd; i >= rowBegin; i--) {
33+
res.push(matrix[i][columnBegin]);
34+
}
35+
}
36+
columnBegin++;
37+
}
38+
39+
return res;
40+
};
41+
42+
spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); //?
43+
spiralOrder([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]); //?

0 commit comments

Comments
 (0)