mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加(0045.跳跃游戏II.md):增加typescript版本
This commit is contained in:
@ -250,6 +250,27 @@ var jump = function(nums) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function jump(nums: number[]): number {
|
||||||
|
const length: number = nums.length;
|
||||||
|
let curFarthestIndex: number = 0,
|
||||||
|
nextFarthestIndex: number = 0;
|
||||||
|
let curIndex: number = 0;
|
||||||
|
let stepNum: number = 0;
|
||||||
|
while (curIndex < length - 1) {
|
||||||
|
nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
|
||||||
|
if (curIndex === curFarthestIndex) {
|
||||||
|
curFarthestIndex = nextFarthestIndex;
|
||||||
|
stepNum++;
|
||||||
|
}
|
||||||
|
curIndex++;
|
||||||
|
}
|
||||||
|
return stepNum;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user