Skip to content

Commit 189349e

Browse files
authored
feat: add rust solution to lc problem: No.0055 (doocs#1293)
1 parent 4f5b102 commit 189349e

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/0000-0099/0055.Jump Game/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,43 @@ public:
113113
};
114114
```
115115
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+
116153
### **Go**
117154

118155
```go

solution/0000-0099/0055.Jump Game/README_EN.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,43 @@ public:
9595
};
9696
```
9797
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+
98135
### **Go**
99136

100137
```go
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)