Skip to content

Commit 67bdf86

Browse files
committedMay 5, 2021
feat: resolved
1 parent 59c4812 commit 67bdf86

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed
 

‎jump_game_ii/src/lib.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
1+
pub struct Solution {}
2+
3+
impl Solution {
4+
pub fn jump(nums: Vec<i32>) -> i32 {
5+
let mut target = nums.len() - 1;
6+
let mut jumps = 0;
7+
'target_loop: loop {
8+
if target == 0 {
9+
break;
10+
}
11+
for cursor in 0..target {
12+
if cursor + nums[cursor] as usize >= target {
13+
jumps += 1;
14+
target = cursor;
15+
continue 'target_loop;
16+
}
17+
}
18+
}
19+
jumps
20+
}
21+
}
22+
123
#[cfg(test)]
2-
mod tests {
24+
mod test {
25+
use super::*;
26+
27+
#[test]
28+
fn example_1() {
29+
let nums = vec![2, 3, 1, 1, 4];
30+
assert_eq!(Solution::jump(nums), 2);
31+
}
32+
333
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
34+
fn example_2() {
35+
let nums = vec![2, 3, 0, 1, 4];
36+
assert_eq!(Solution::jump(nums), 2);
637
}
738
}

0 commit comments

Comments
 (0)
Please sign in to comment.