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
|
fn main() {
println!("Hello, world!");
let tests = [
(121, true),
(-121, false),
(10, false),
(1410110141, true)
];
for test in tests {
println!("{} is {} should be {}", test.0, Solution::is_palindrome(test.0), test.1);
}
}
struct Solution {}
impl Solution {
pub fn is_palindrome(x: i32) -> bool {
if x < 0 {return false;}
let len = (x as f32).log10() as usize;
//println!("{} {}", len, (len+1)/2);
for i in 0..(len+1)/2 {
if (x as u32 / 10_u32.pow(i as u32)) % 10 != (x as u32 / 10_u32.pow((len - i) as u32)) % 10 {
return false;
}
}
true
}
pub fn get(x: i32, i: usize) -> u32 {
(x as u32 / 10_u32.pow(i as u32)) % 10
}
}
|