File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,5 @@ mod longest_substring_without_repeating_characters;
2323mod sort_colors;
2424mod reverse_integer;
2525mod integer_to_roman;
26- mod string_to_integer_atoi;
26+ mod string_to_integer_atoi;
27+ mod palindrome_number;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments