Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1011 from xiaofei-2020/ts3
Browse files Browse the repository at this point in the history
添加(0059.螺旋矩阵II.md):增加typescript版本
  • Loading branch information
youngyangyang04 authored Jan 19, 2022
2 parents 1e68f64 + 552388e commit 294ca5b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions problems/0059.螺旋矩阵II.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,51 @@ var generateMatrix = function(n) {

```

TypeScript:

```typescript
function generateMatrix(n: number): number[][] {
let loopNum: number = Math.floor(n / 2);
const resArr: number[][] = new Array(n).fill(1).map(i => new Array(n));
let chunkNum: number = n - 1;
let startX: number = 0;
let startY: number = 0;
let value: number = 1;
let x: number, y: number;
while (loopNum--) {
x = startX;
y = startY;
while (x < startX + chunkNum) {
resArr[y][x] = value;
x++;
value++;
}
while (y < startY + chunkNum) {
resArr[y][x] = value;
y++;
value++;
}
while (x > startX) {
resArr[y][x] = value;
x--;
value++;
}
while (y > startY) {
resArr[y][x] = value;
y--;
value++;
}
startX++;
startY++;
chunkNum -= 2;
}
if (n % 2 === 1) {
resArr[startX][startY] = value;
}
return resArr;
};
```

Go:

```go
Expand Down

0 comments on commit 294ca5b

Please sign in to comment.