File tree Expand file tree Collapse file tree 1 file changed +51
-2
lines changed Expand file tree Collapse file tree 1 file changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -46,8 +46,8 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum
4646
4747## 代码
4848
49- - 语言支持: Javascript, Python3
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+
68117Python3 Code:
69118
70119``` Python
You can’t perform that action at this time.
0 commit comments