Find the Closest Palindrome LeetCode solution in Rust

1
// Mfw I can't do a q. in > 12 lines:
2

3
impl Solution {
4
pub fn nearest_palindromic(s: String) -> String {
5
let num = s.parse::<i64>().unwrap();
6
let len = s.len();
7

8
let half = s[0..(len + 1) / 2].parse::<i64>().unwrap();
9

10
let mut candidates = vec![10i64.pow(len as u32) + 1, 10i64.pow(len as u32 - 1) - 1];
11

12
for &i in &[half - 1, half, half + 1] {
13
let mut cand_str = i.to_string();
14
let rev = cand_str.chars().rev().collect::<String>();
15

16
if len % 2 != 0 {
17
cand_str.pop();
18
}
19

20
cand_str.push_str(&rev);
21
candidates.push(cand_str.parse().unwrap());
22
}
23

24
candidates
25
.into_iter()
26
.filter(|&c| c != num)
27
.min_by_key(|&c| ((c - num).abs(), c))
28
.unwrap()
29
.to_string()
30
}
31
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0