mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0045.跳跃游戏II.md
This commit is contained in:
@ -379,43 +379,46 @@ object Solution {
|
|||||||
```Rust
|
```Rust
|
||||||
//版本一
|
//版本一
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn max(a: i32, b:i32) -> i32 {
|
|
||||||
if a > b { a } else { b }
|
|
||||||
}
|
|
||||||
pub fn jump(nums: Vec<i32>) -> i32 {
|
pub fn jump(nums: Vec<i32>) -> i32 {
|
||||||
if nums.len() == 0 { return 0; }
|
if nums.len() == 1 {
|
||||||
let mut cur_distance: i32 = 0;
|
return 0;
|
||||||
let mut ans: i32 = 0;
|
}
|
||||||
let mut next_distance: i32 = 0;
|
let mut cur_distance = 0;
|
||||||
for i in 0..nums.len() {
|
let mut ans = 0;
|
||||||
next_distance = Self::max(nums[i] + i as i32, next_distance);
|
let mut next_distance = 0;
|
||||||
if i as i32 == cur_distance {
|
for (n, &i) in nums.iter().enumerate() {
|
||||||
if cur_distance != (nums.len() - 1) as i32 {
|
next_distance = (n as i32 + i).max(next_distance);
|
||||||
|
if i == cur_distance {
|
||||||
|
if cur_distance < n as i32 - 1 {
|
||||||
ans += 1;
|
ans += 1;
|
||||||
cur_distance = next_distance;
|
cur_distance = next_distance;
|
||||||
if next_distance == (nums.len() - 1) as i32 { break; }
|
if next_distance >= n as i32 - 1 {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else { break; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ans
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
//版本二
|
//版本二
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn max(a: i32, b:i32) -> i32 {
|
|
||||||
if a > b { a } else { b }
|
|
||||||
}
|
|
||||||
pub fn jump(nums: Vec<i32>) -> i32 {
|
pub fn jump(nums: Vec<i32>) -> i32 {
|
||||||
let mut cur_distance: i32 = 0;
|
if nums.len() == 1 {
|
||||||
let mut ans: i32 = 0;
|
return 0;
|
||||||
let mut next_distance: i32 = 0;
|
}
|
||||||
for i in 0..nums.len() - 1 {
|
let mut cur_distance = 0;
|
||||||
next_distance = Self::max(nums[i] + i as i32, next_distance);
|
let mut ans = 0;
|
||||||
if i as i32 == cur_distance {
|
let mut next_distance = 0;
|
||||||
|
for (n, &i) in nums.iter().enumerate() {
|
||||||
|
next_distance = (n as i32 + i).max(next_distance);
|
||||||
|
if i == cur_distance {
|
||||||
cur_distance = next_distance;
|
cur_distance = next_distance;
|
||||||
ans += 1;
|
ans += 1;
|
||||||
}
|
}
|
||||||
@ -423,6 +426,7 @@ impl Solution {
|
|||||||
ans
|
ans
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user