Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4f4f837

Browse files
add: Spiral Matrix
1 parent 05d5d3a commit 4f4f837

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2222
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
2323
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [JavaScript](./src/search-for-a-range/res.js)|Medium|
2424
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js)|Medium|
25+
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
2526
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
2627
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
2728
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|

‎src/spiral-matrix/res.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
3+
*
4+
* For example,
5+
*
6+
* Given the following matrix:
7+
* [
8+
* [ 1, 2, 3 ],
9+
* [ 4, 5, 6 ],
10+
* [ 7, 8, 9 ]
11+
* ]
12+
*
13+
* You should return [1,2,3,6,9,8,7,4,5].
14+
*
15+
* res.js
16+
* @authors Joe Jiang (hijiangtao@gmail.com)
17+
* @date 2017年03月03日 21:42:58
18+
* @version $Id$
19+
*
20+
* @param {number[][]} matrix
21+
* @return {number[]}
22+
*/
23+
let spiralOrder = function(matrix) {
24+
if (matrix.length === 0) {
25+
return matrix;
26+
}
27+
28+
let rowBegin = 0,
29+
rowEnd = matrix.length-1,
30+
colBegin = 0,
31+
colEnd = matrix[0].length-1,
32+
res =[];
33+
34+
while (rowBegin <= rowEnd || colBegin <= colEnd) {
35+
// left->right
36+
for (let i=colBegin; i<=colEnd; i++) {
37+
res.push( matrix[rowBegin][i] );
38+
}
39+
if (rowBegin === rowEnd) {
40+
break;
41+
}
42+
43+
// right->bottom
44+
rowBegin++;
45+
for (let i=rowBegin; i<=rowEnd; i++) {
46+
res.push( matrix[i][colEnd] );
47+
}
48+
if (colBegin === colEnd) {
49+
break;
50+
}
51+
52+
// bottom->left
53+
colEnd--;
54+
for (let i=colEnd; i>=colBegin; i--) {
55+
res.push( matrix[rowEnd][i] );
56+
}
57+
if (rowBegin === rowEnd) {
58+
break;
59+
}
60+
61+
// left bottom->top
62+
rowEnd--;
63+
for (let i=rowEnd; i>=rowBegin; i--) {
64+
res.push( matrix[i][colBegin] );
65+
}
66+
if (colBegin === colEnd) {
67+
break;
68+
}
69+
colBegin++;
70+
}
71+
72+
return res;
73+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /