Участник:Evgin/super palindromes rs

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/super-palindromes/

Rust

impl Solution {
    pub fn superpalindromes_in_range(l: String, r: String) -> i32 {
        let start = l.parse::<i64>().unwrap();
        let end = r.parse::<i64>().unwrap();
        let map = (1..=100000).map(|i| i.to_string());
 
        count_palindromes(map, true, start, end, 0)
    }
 
}
 
fn is_palindrome(x: i64) -> bool {
    let mut n = x;
    let mut rev = 0;
 
    while n > 0 {
        rev = 10 * rev + n % 10;
        n /= 10;
    }
    rev == x
}
 
fn count_palindromes(
    iter: impl Iterator<Item=String> + std::clone::Clone,
    even: bool,
    start: i64,
    end: i64,
    mut count: i32
) -> i32 {
 
    let offset = { if even { 0 } else { 1 } };
    let cln = iter.clone();
    count += cln.map(|s| s.chars().chain(s.chars().rev().skip(offset)).collect::<String>())
            .map(|s| s.parse::<i64>().unwrap())
            .map(|i| i * i)
            .take_while(|&sqr| sqr <= end)
            .filter(|&sqr| sqr >= start && is_palindrome(sqr))
            .count() as i32;
 
    if even {
        count_palindromes(iter, false, start, end, count)
    } else {
        count
    }
 
}