-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1920: Build Array from Permutation
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pub mod utilize_higher_bits; | ||
|
||
pub trait Solution { | ||
fn build_array(nums: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[0, 2, 1, 5, 3, 4] as &[_], &[0, 1, 2, 4, 5, 3] as &[_]), | ||
(&[5, 0, 1, 2, 3, 4], &[4, 5, 0, 1, 2, 3]), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::build_array(nums.to_vec()), expected); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/problem_1920_build_array_from_permutation/utilize_higher_bits.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn build_array(nums: Vec<i32>) -> Vec<i32> { | ||
let mut nums = nums; | ||
let slice = nums.as_mut_slice(); | ||
|
||
for i in 0..slice.len() { | ||
slice[i] |= slice[slice[i] as u32 as usize] << 16; | ||
} | ||
|
||
for num in slice { | ||
*num >>= 16; | ||
*num &= 0x_ffff; | ||
} | ||
|
||
nums | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn build_array(nums: Vec<i32>) -> Vec<i32> { | ||
Self::build_array(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |