Skip to content

Commit 67cda7a

Browse files
committed
solve problem Spiral Matrix II
1 parent f477bee commit 67cda7a

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ All solutions will be accepted!
260260
|338|[Counting Bits](https://leetcode-cn.com/problems/counting-bits/description/)|[java/py/js](./algorithms/CountingBits)|Medium|
261261
|287|[Find The Duplicate Number](https://leetcode-cn.com/problems/find-the-duplicate-number/description/)|[java/py/js](./algorithms/FindTheDuplicateNumber)|Medium|
262262
|807|[Max Increase To Keep City Skyline](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/description/)|[java/py/js](./algorithms/MaxIncreaseToKeepCitySkyline)|Medium|
263+
|59|[Spiral Matrix II](https://leetcode-cn.com/problems/spiral-matrix-ii/description/)|[java/py/js](./algorithms/SpiralMatrixII)|Medium|
263264

264265
# Database
265266
|#|Title|Solution|Difficulty|
266267
|:-|:-|:-|:-|
267268
|596|[Big Countries](https://leetcode-cn.com/problems/big-countries/description/)|[mysql](./database/BigCountries)|Easy|
268-
|182|[Duplicate Emails](https://leetcode-cn.com/problems/duplicate-emails/description/)|[mysql](./algorithms/DuplicateEmails)|Easy|
269+
|182|[Duplicate Emails](https://leetcode-cn.com/problems/duplicate-emails/description/)|[mysql](./database/DuplicateEmails)|Easy|

algorithms/SpiralMatrixII/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Spiral Matrix II
2+
This problem is easy to solve
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public int[][] generateMatrix(int n) {
3+
int[][] matrix = new int[n][n];
4+
5+
int i = 0,
6+
j = 0,
7+
topLimit = 0,
8+
leftLimit = 0,
9+
bottomLimit = n - 1,
10+
rightLimit = n - 1,
11+
xDirection = 1,
12+
yDirection = 0;
13+
14+
for (int v = 1, max = (int) Math.pow(n, 2); v <= max; v++) {
15+
matrix[i][j] = v;
16+
17+
if (j + xDirection > rightLimit) {
18+
topLimit = i + 1;
19+
xDirection = 0;
20+
yDirection = 1;
21+
} else if (i + yDirection > bottomLimit) {
22+
rightLimit = i - 1;
23+
xDirection = -1;
24+
yDirection = 0;
25+
} else if (j + xDirection < leftLimit) {
26+
bottomLimit = i - 1;
27+
xDirection = 0;
28+
yDirection = -1;
29+
} else if (i + yDirection < topLimit) {
30+
leftLimit = j + 1;
31+
xDirection = 1;
32+
yDirection = 0;
33+
}
34+
35+
i += yDirection;
36+
j += xDirection;
37+
}
38+
return matrix;
39+
}
40+
}

algorithms/SpiralMatrixII/solution.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {number} n
3+
* @return {number[][]}
4+
*/
5+
var generateMatrix = function(n) {
6+
let matrix = []
7+
8+
for (let i = 0; i < n; i++) {
9+
matrix.push(new Array(n));
10+
}
11+
12+
let i = j = 0,
13+
topLimit = leftLimit = 0,
14+
bottomLimit = rightLimit = n - 1,
15+
xDirection = 1,
16+
yDirection = 0
17+
18+
for (let v = 1; v <= Math.pow(n, 2); v++) {
19+
matrix[i][j] = v
20+
21+
if (j + xDirection > rightLimit) {
22+
topLimit = i + 1
23+
xDirection = 0
24+
yDirection = 1
25+
} else if (i + yDirection > bottomLimit) {
26+
rightLimit = i - 1
27+
xDirection = -1
28+
yDirection = 0
29+
} else if (j + xDirection < leftLimit) {
30+
bottomLimit = i - 1
31+
xDirection = 0
32+
yDirection = -1
33+
} else if (i + yDirection < topLimit) {
34+
leftLimit = j + 1
35+
xDirection = 1
36+
yDirection = 0
37+
}
38+
39+
i += yDirection
40+
j += xDirection
41+
}
42+
return matrix
43+
};

algorithms/SpiralMatrixII/solution.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution(object):
2+
def generateMatrix(self, n):
3+
"""
4+
:type n: int
5+
:rtype: List[List[int]]
6+
"""
7+
matrix = []
8+
for i in xrange(n):
9+
matrix.append([0] * n)
10+
11+
i = j = 0
12+
13+
top_limit = left_limit = 0
14+
bottom_limit = right_limit = n - 1
15+
16+
x_direction = 1
17+
y_direction = 0
18+
19+
for v in xrange(1, pow(n, 2) + 1):
20+
matrix[i][j] = v
21+
22+
if j + x_direction > right_limit:
23+
top_limit = i + 1
24+
x_direction = 0
25+
y_direction = 1
26+
elif i + y_direction > bottom_limit:
27+
right_limit = i - 1
28+
x_direction = -1
29+
y_direction = 0
30+
elif j + x_direction < left_limit:
31+
bottom_limit = i - 1
32+
x_direction = 0
33+
y_direction = -1
34+
elif i + y_direction < top_limit:
35+
left_limit = j + 1
36+
x_direction = 1
37+
y_direction = 0
38+
39+
i += y_direction
40+
j += x_direction
41+
42+
return generateMatrix

0 commit comments

Comments
 (0)