mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0045.跳跃游戏II.md
0045.跳跃游戏||新增C语言实现
This commit is contained in:
@ -492,7 +492,34 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C
|
||||
|
||||
```c
|
||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
int jump(int* nums, int numsSize) {
|
||||
if(numsSize == 1){
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
// 记录当前能走的最远距离
|
||||
int curDistance = 0;
|
||||
// 记录下一步能走的最远距离
|
||||
int nextDistance = 0;
|
||||
for(int i = 0; i < numsSize; i++){
|
||||
nextDistance = max(i + nums[i], nextDistance);
|
||||
// 下标到了当前的最大距离
|
||||
if(i == nextDistance){
|
||||
count++;
|
||||
curDistance = nextDistance;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### C#
|
||||
|
||||
```csharp
|
||||
// 版本二
|
||||
public class Solution
|
||||
@ -518,3 +545,4 @@ public class Solution
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user