Skip to content

Commit 683ca1b

Browse files
authored
feat(ml): Update 55.jump-game.md (#417)
添加C++、Java支持
1 parent 0ab87ff commit 683ca1b

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

problems/55.jump-game.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum
4646

4747
## 代码
4848

49-
- 语言支持: JavascriptPython3
50-
49+
- 语言支持: Javascript,C++,Java,Python3
50+
Javascript Code:
5151
```js
5252
/**
5353
* @param {number[]} nums
@@ -65,6 +65,55 @@ var canJump = function (nums) {
6565
};
6666
```
6767

68+
C++ Code:
69+
70+
```c++
71+
class Solution {
72+
public:
73+
bool canJump(vector<int>& nums) {
74+
int n=nums.size();
75+
int k=0;
76+
for(int i=0;i<n;i++)
77+
{
78+
if(i>k){
79+
return false;
80+
}
81+
// 能跳到最后一个位置
82+
if(k>=n-1){
83+
return true;
84+
}
85+
// 从当前位置能跳的最远的位置
86+
k = max(k, i+nums[i]);
87+
}
88+
return k >= n-1;
89+
}
90+
};
91+
```
92+
93+
Java Code:
94+
95+
```java
96+
class Solution {
97+
public boolean canJump(int[] nums) {
98+
int n=nums.length;
99+
int k=0;
100+
for(int i=0;i<n;i++)
101+
{
102+
if(i>k){
103+
return false;
104+
}
105+
// 能跳到最后一个位置
106+
if(k>=n-1){
107+
return true;
108+
}
109+
// 从当前位置能跳的最远的位置
110+
k = Math.max(k, i+nums[i]);
111+
}
112+
return k >= n-1;
113+
}
114+
}
115+
```
116+
68117
Python3 Code:
69118

70119
```Python

0 commit comments

Comments
 (0)