Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

59. Spiral Matrix II

Posted on 2020-07-04 | Edited on 2021-01-22

LeetCode

link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Java O(N^2)
class Solution {
public int[][] generateMatrix(int n) {
int[][] ret = new int[n][n];
int left = -1,right = n -1;
int top = 0,down = n - 1;
int count = 1;
while (left <= right) {
for (int i = ++left; i <= right; i++) ret[top][i] = count++;
for (int i = ++top; i <= down; i++) ret[i][right] = count++;
for (int i = --right; i >= left; i--) ret[down][i] = count++;
for (int i = --down; i >= top; i--) ret[i][left] = count++;
}
return ret;
}
}

<1…878889…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%