Skip to content

Commit 9a9f039

Browse files
committed
Palindrome Number
1 parent b20633a commit 9a9f039

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ mod longest_substring_without_repeating_characters;
2323
mod sort_colors;
2424
mod reverse_integer;
2525
mod integer_to_roman;
26-
mod string_to_integer_atoi;
26+
mod string_to_integer_atoi;
27+
mod palindrome_number;

src/palindrome_number.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// @number 9
2+
/// @title Palindrome Number
3+
/// @url https://leetcode.com/problems/palindrome-number/
4+
/// @difficulty easy
5+
6+
7+
struct Solution;
8+
9+
10+
impl Solution {
11+
pub fn is_palindrome(x: i32) -> bool {
12+
if x < 0 { return false; }
13+
14+
let string = x.to_string();
15+
let mut chars: Vec<char> = string.chars().collect();
16+
while chars.len() > 1 {
17+
let last = chars.pop().unwrap();
18+
let first = chars.remove(0);
19+
if first != last {
20+
return false;
21+
}
22+
}
23+
true
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod test {
29+
use crate::palindrome_number::Solution;
30+
31+
#[test]
32+
fn should_return_true_given_121() {
33+
assert_eq!(true, Solution::is_palindrome(121));
34+
}
35+
#[test]
36+
fn should_return_false_given_negative_number() {
37+
assert_eq!(false, Solution::is_palindrome(-121));
38+
}
39+
#[test]
40+
fn should_return_true_given_zero() {
41+
assert_eq!(true, Solution::is_palindrome(0));
42+
}
43+
44+
#[test]
45+
fn should_return_false_given_10() {
46+
assert_eq!(false, Solution::is_palindrome(10));
47+
}
48+
}

0 commit comments

Comments
 (0)