diff --git a/solution/0000-0099/0055.Jump Game/README.md b/solution/0000-0099/0055.Jump Game/README.md index 2bdf490eaec89..5a8f8c1298a86 100644 --- a/solution/0000-0099/0055.Jump Game/README.md +++ b/solution/0000-0099/0055.Jump Game/README.md @@ -118,29 +118,13 @@ impl Solution { #[allow(dead_code)] pub fn can_jump(nums: Vec) -> bool { let n = nums.len(); - let mut i: usize = 0; + let mut mx = 0; - while i < n { - if nums[i] as usize + i >= n - 1 { - break; - } - let mut j: usize = 1; - let mut max_step = 0; - let mut next_i: usize = 0; - // Get the next max step - while j <= nums[i] as usize { - if (i + j) as i32 + nums[i + j] >= max_step { - max_step = (i + j) as i32 + nums[i + j]; - next_i = i + j; - } - j += 1; - } - if max_step == 0 { - // No further max step + for i in 0..n { + if mx < i { return false; } - // Otherwise, update `i` to `next_i` - i = next_i; + mx = std::cmp::max(mx, i + nums[i] as usize); } true diff --git a/solution/0000-0099/0055.Jump Game/README_EN.md b/solution/0000-0099/0055.Jump Game/README_EN.md index 5b438738130e0..ba8fb69db80e7 100644 --- a/solution/0000-0099/0055.Jump Game/README_EN.md +++ b/solution/0000-0099/0055.Jump Game/README_EN.md @@ -102,29 +102,13 @@ impl Solution { #[allow(dead_code)] pub fn can_jump(nums: Vec) -> bool { let n = nums.len(); - let mut i: usize = 0; + let mut mx = 0; - while i < n { - if nums[i] as usize + i >= n - 1 { - break; - } - let mut j: usize = 1; - let mut max_step = 0; - let mut next_i: usize = 0; - // Get the next max step - while j <= nums[i] as usize { - if (i + j) as i32 + nums[i + j] >= max_step { - max_step = (i + j) as i32 + nums[i + j]; - next_i = i + j; - } - j += 1; - } - if max_step == 0 { - // No further max step + for i in 0..n { + if mx < i { return false; } - // Otherwise, update `i` to `next_i` - i = next_i; + mx = std::cmp::max(mx, i + nums[i] as usize); } true diff --git a/solution/0000-0099/0055.Jump Game/Solution.rs b/solution/0000-0099/0055.Jump Game/Solution.rs index 0eafbf94ea0e1..e13d0c3f19fc2 100644 --- a/solution/0000-0099/0055.Jump Game/Solution.rs +++ b/solution/0000-0099/0055.Jump Game/Solution.rs @@ -2,29 +2,13 @@ impl Solution { #[allow(dead_code)] pub fn can_jump(nums: Vec) -> bool { let n = nums.len(); - let mut i: usize = 0; + let mut mx = 0; - while i < n { - if nums[i] as usize + i >= n - 1 { - break; - } - let mut j: usize = 1; - let mut max_step = 0; - let mut next_i: usize = 0; - // Get the next max step - while j <= nums[i] as usize { - if (i + j) as i32 + nums[i + j] >= max_step { - max_step = (i + j) as i32 + nums[i + j]; - next_i = i + j; - } - j += 1; - } - if max_step == 0 { - // No further max step + for i in 0..n { + if mx < i { return false; } - // Otherwise, update `i` to `next_i` - i = next_i; + mx = std::cmp::max(mx, i + nums[i] as usize); } true