-
Notifications
You must be signed in to change notification settings - Fork 111
/
super-palindromes.rs
48 lines (47 loc) · 1.42 KB
/
super-palindromes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Super Palindromes
impl Solution {
pub fn superpalindromes_in_range(left: String, right: String) -> i32 {
fn f(mut i: i64, mut j: i64) -> i64 {
while j > 0 {
i = i*10+j%10;
j /= 10;
}
i
}
fn is(i: i64) -> bool {
let mut t = i;
let mut j = 0;
while t > 0 {
j = j*10+t%10;
t /= 10;
}
i == j
}
let left = left.parse::<i64>().unwrap();
let right = right.parse::<i64>().unwrap();
let mut sqrtl = (left as f64).sqrt() as i64;
let mut sqrtr = (right as f64).sqrt() as i64;
if sqrtl*sqrtl < left { sqrtl += 1; }
if sqrtr*sqrtr > right { sqrtr -= 1; }
let mut i = 1;
let mut x = 0;
let mut ans = 0;
while x <= sqrtr {
x = f(i, i);
if x >= sqrtl && x <= sqrtr && is(x*x) { ans += 1; }
x = f(i, i/10);
if x >= sqrtl && x <= sqrtr && is(x*x) { ans += 1; }
i += 1;
}
ans
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test() {
assert_eq!(Solution::superpalindromes_in_range("38455498359".to_string(), "1000000002000000000".to_string()), 45);
assert_eq!(Solution::superpalindromes_in_range("38455498359".to_string(), "1000000002000000001".to_string()), 46);
}
}