-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0055_jump_game.rs
33 lines (29 loc) · 924 Bytes
/
s0055_jump_game.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
struct Solution;
//leetcode submit region begin(Prohibit modification and deletion)
impl Solution {
pub fn can_jump(nums: Vec<i32>) -> bool {
let n = nums.len();
let mut max_distance = 0_usize;
for i in 0..n {
if i > max_distance {
return false;
}
let distance = i + nums[i] as usize;
max_distance = usize::max(max_distance, distance);
}
true
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
assert_eq!(Solution::can_jump(vec![2, 3, 1, 1, 4]), true);
assert_eq!(Solution::can_jump(vec![3, 2, 1, 0, 4]), false);
assert_eq!(Solution::can_jump(vec![0]), true);
assert_eq!(Solution::can_jump(vec![0, 2, 3]), false);
assert_eq!(Solution::can_jump(vec![1, 0, 1, 0]), false);
}
}