/
reverse_half.rs
42 lines (35 loc) · 983 Bytes
/
reverse_half.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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
pub fn is_palindrome(x: i32) -> bool {
if x < 0 {
false
} else if x == 0 {
true
} else if x % 10 == 0 {
false
} else {
let mut x = x;
let mut reversed = 0;
while reversed < x {
reversed *= 10;
reversed += x % 10;
x /= 10;
}
reversed == x || reversed / 10 == x
}
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn is_palindrome(x: i32) -> bool {
Self::is_palindrome(x)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}