File tree Expand file tree Collapse file tree 3 files changed +106
-0
lines changed
solution/0000-0099/0055.Jump Game Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,43 @@ public:
113
113
};
114
114
```
115
115
116
+ ### **Rust**
117
+
118
+ ```rust
119
+ impl Solution {
120
+ #[allow(dead_code)]
121
+ pub fn can_jump(nums: Vec<i32>) -> bool {
122
+ let n = nums.len();
123
+ let mut i: usize = 0;
124
+
125
+ while i < n {
126
+ if nums[i] as usize + i >= n - 1 {
127
+ break;
128
+ }
129
+ let mut j: usize = 1;
130
+ let mut max_step = 0;
131
+ let mut next_i: usize = 0;
132
+ // Get the next max step
133
+ while j <= nums[i] as usize {
134
+ if (i + j) as i32 + nums[i + j] >= max_step {
135
+ max_step = (i + j) as i32 + nums[i + j];
136
+ next_i = i + j;
137
+ }
138
+ j += 1;
139
+ }
140
+ if max_step == 0 {
141
+ // No further max step
142
+ return false;
143
+ }
144
+ // Otherwise, update `i` to `next_i`
145
+ i = next_i;
146
+ }
147
+
148
+ true
149
+ }
150
+ }
151
+ ```
152
+
116
153
### ** Go**
117
154
118
155
``` go
Original file line number Diff line number Diff line change @@ -95,6 +95,43 @@ public:
95
95
};
96
96
```
97
97
98
+ ### **Rust**
99
+
100
+ ```rust
101
+ impl Solution {
102
+ #[allow(dead_code)]
103
+ pub fn can_jump(nums: Vec<i32>) -> bool {
104
+ let n = nums.len();
105
+ let mut i: usize = 0;
106
+
107
+ while i < n {
108
+ if nums[i] as usize + i >= n - 1 {
109
+ break;
110
+ }
111
+ let mut j: usize = 1;
112
+ let mut max_step = 0;
113
+ let mut next_i: usize = 0;
114
+ // Get the next max step
115
+ while j <= nums[i] as usize {
116
+ if (i + j) as i32 + nums[i + j] >= max_step {
117
+ max_step = (i + j) as i32 + nums[i + j];
118
+ next_i = i + j;
119
+ }
120
+ j += 1;
121
+ }
122
+ if max_step == 0 {
123
+ // No further max step
124
+ return false;
125
+ }
126
+ // Otherwise, update `i` to `next_i`
127
+ i = next_i;
128
+ }
129
+
130
+ true
131
+ }
132
+ }
133
+ ```
134
+
98
135
### ** Go**
99
136
100
137
``` go
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ #[ allow( dead_code) ]
3
+ pub fn can_jump ( nums : Vec < i32 > ) -> bool {
4
+ let n = nums. len ( ) ;
5
+ let mut i: usize = 0 ;
6
+
7
+ while i < n {
8
+ if nums[ i] as usize + i >= n - 1 {
9
+ break ;
10
+ }
11
+ let mut j: usize = 1 ;
12
+ let mut max_step = 0 ;
13
+ let mut next_i: usize = 0 ;
14
+ // Get the next max step
15
+ while j <= nums[ i] as usize {
16
+ if ( i + j) as i32 + nums[ i + j] >= max_step {
17
+ max_step = ( i + j) as i32 + nums[ i + j] ;
18
+ next_i = i + j;
19
+ }
20
+ j += 1 ;
21
+ }
22
+ if max_step == 0 {
23
+ // No further max step
24
+ return false ;
25
+ }
26
+ // Otherwise, update `i` to `next_i`
27
+ i = next_i;
28
+ }
29
+
30
+ true
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments