-
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 2217: Find Palindrome With Fixed Length
- Loading branch information
Showing
3 changed files
with
137 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
73 changes: 73 additions & 0 deletions
73
src/problem_2217_find_palindrome_with_fixed_length/mathematical.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,73 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn reverse(mut x: u32) -> u32 { | ||
let mut result = 0; | ||
|
||
loop { | ||
result = result * 10 + x % 10; | ||
x /= 10; | ||
|
||
if x == 0 { | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
pub fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64> { | ||
let int_length = int_length as u32; | ||
let half_int_length = int_length / 2; | ||
let other_half_int_length = int_length - half_int_length; | ||
let first = u32::pow(10, other_half_int_length - 1); | ||
let count = first * 9; | ||
let offset = first - 1; | ||
let shift = u64::from(u32::pow(10, half_int_length)); | ||
let iter = queries.into_iter(); | ||
|
||
if int_length & 1 == 0 { | ||
iter.map(|query| { | ||
let query_u32 = query as u32; | ||
|
||
if query_u32 <= count { | ||
let left = offset + query_u32; | ||
|
||
(u64::from(left) * shift + u64::from(Self::reverse(left))) as _ | ||
} else { | ||
-1 | ||
} | ||
}) | ||
.collect() | ||
} else { | ||
iter.map(|query| { | ||
let query_u32 = query as u32; | ||
|
||
if query_u32 <= count { | ||
let left = offset + query_u32; | ||
|
||
(u64::from(left) * shift + u64::from(Self::reverse(left / 10))) as _ | ||
} else { | ||
-1 | ||
} | ||
}) | ||
.collect() | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64> { | ||
Self::kth_palindrome(queries, int_length) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,63 @@ | ||
pub mod mathematical; | ||
|
||
pub trait Solution { | ||
fn kth_palindrome(queries: Vec<i32>, int_length: i32) -> Vec<i64>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
(&[1, 2, 3, 4, 5, 90] as &[_], 3), | ||
&[101_i64, 111, 121, 131, 141, 999] as &[_], | ||
), | ||
((&[2, 4, 6], 4), &[1111, 1331, 1551]), | ||
( | ||
( | ||
&[ | ||
392_015_495, | ||
5, | ||
4, | ||
1, | ||
425_320_571, | ||
565_971_690, | ||
3, | ||
7, | ||
6, | ||
3, | ||
506_222_280, | ||
468_075_092, | ||
5, | ||
], | ||
2, | ||
), | ||
&[-1, 55, 44, 11, -1, -1, 33, 77, 66, 33, -1, -1, 55], | ||
), | ||
( | ||
( | ||
&[ | ||
2, | ||
201_429_812, | ||
8, | ||
520_498_110, | ||
492_711_727, | ||
339_882_032, | ||
462_074_369, | ||
9, | ||
7, | ||
6, | ||
], | ||
1, | ||
), | ||
&[2, -1, 8, -1, -1, -1, -1, 9, 7, 6], | ||
), | ||
]; | ||
|
||
for ((queries, int_length), expected) in test_cases { | ||
assert_eq!(S::kth_palindrome(queries.to_vec(), int_length), expected); | ||
} | ||
} | ||
} |