diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/jump_game3.rs | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/jump_game3.rs')
-rw-r--r-- | other/jump_game3.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/other/jump_game3.rs b/other/jump_game3.rs new file mode 100644 index 0000000..a973fad --- /dev/null +++ b/other/jump_game3.rs @@ -0,0 +1,35 @@ +use std::collections::VecDeque; +struct Solution; + +impl Solution { + pub fn can_reach(arr: Vec<i32>, start: i32) -> bool { + let mut arr = arr; + let mut vd: VecDeque<i32> = VecDeque::with_capacity(arr.len()); + vd.push_back(start); + while let Some(front) = vd.pop_front() { + match arr[front as usize].cmp(&0) { + std::cmp::Ordering::Less => continue, + std::cmp::Ordering::Equal => return true, + std::cmp::Ordering::Greater => { + if front - arr[front as usize] >= 0 { + vd.push_back(front - arr[front as usize]); + } + if front + arr[front as usize] < arr.len() as i32 { + vd.push_back(front + arr[front as usize]); + } + arr[front as usize] = -1; + } + } + } + false + } +} + +fn main() { + let arr1 = vec![4,2,3,0,3,1,2]; + let arr2 = vec![4,2,3,0,3,1,2]; + let arr3 = vec![3,0,2,1,2]; + println!("{}", Solution::can_reach(arr1, 5)); //true + println!("{}", Solution::can_reach(arr2, 0)); //true + println!("{}", Solution::can_reach(arr3, 2)); //false +} |