Update 0045.跳跃游戏II.md

0045.跳跃游戏||新增C语言实现
This commit is contained in:
a12bb
2024-02-26 20:47:23 +08:00
parent b132f5273d
commit 415b031025

View File

@ -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# ### C#
```csharp ```csharp
// 版本二 // 版本二
public class Solution public class Solution
@ -518,3 +545,4 @@ public class Solution
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>