Skip to content

Commit

Permalink
Update: Leetcode 250207
Browse files Browse the repository at this point in the history
  • Loading branch information
HuangFuSL committed Feb 7, 2025
1 parent f2dd044 commit ebefc70
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion docs/coding/leetcode/59.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void change(int direction, int *i, int *j, int incre)
int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
*returnSize = n;
*returnColumnSizes = (int *)malloc(sizeof(int) * n);

int i = 0, j = 0, m = 1, direction = 0, **ret = (int **)malloc(sizeof(int *) * n);
for (i = 0; i < n; i++)
{
Expand All @@ -90,3 +90,22 @@ int** generateMatrix(int n, int* returnSize, int** returnColumnSizes){
return ret;
}
```
```python
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
result = [[0] * n for _ in range(n)]
i, j, step, total = 0, 0, 2, n * n
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] * n
result[i][j] = 1
for d_i, d_j in directions:
i, j = i + d_i, j + d_j
while i >= 0 and i < n and j >= 0 and j < n:
if result[i][j]:
break
result[i][j] = step
step += 1
i, j = i + d_i, j + d_j
i, j = i - d_i, j - d_j
return result
```

0 comments on commit ebefc70

Please sign in to comment.