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
46
46
47
47
## 代码
48
48
49
- - 语言支持: Javascript, Python3
50
-
49
+ - 语言支持: Javascript,C++,Java, Python3
50
+ Javascript Code:
51
51
``` js
52
52
/**
53
53
* @param {number[]} nums
@@ -65,6 +65,55 @@ var canJump = function (nums) {
65
65
};
66
66
```
67
67
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
+
68
117
Python3 Code:
69
118
70
119
``` Python
You can’t perform that action at this time.
0 commit comments