Skip to content

Commit

Permalink
Palindrome number (#17)
Browse files Browse the repository at this point in the history
* add problem polindrome number

* add solution polindrome number

* impl solution polindrome number
  • Loading branch information
SKTT1Ryze authored Oct 26, 2023
1 parent 2295378 commit 0c35892
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions leetcode/src/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod add_two_numbers;
pub mod longest_palindromic_substring;
pub mod longest_substring;
pub mod median_of_two_sorted_arrays;
pub mod palindrome_number;
pub mod reverse_integer;
pub mod string_to_integer;
pub mod two_sum;
Expand Down
13 changes: 13 additions & 0 deletions leetcode/src/problems/palindrome_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::{Difficulty, Problem, Topic};

pub struct ProblemImpl;

crate::derive_problem!(
ProblemImpl,
9,
Difficulty::Easy,
Topic::Algorithms,
"Palindrome Number",
"Given an integer x, return true if x is a palindrome, and false otherwise.",
"Match".into()
);
1 change: 1 addition & 0 deletions leetcode/src/solutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod add_two_numbers;
pub mod longest_palindromic_substring;
pub mod longest_substring;
pub mod median_of_two_sorted_arrays;
pub mod palindrome_number;
pub mod reverse_integer;
pub mod string_to_integer;
pub mod two_sum;
Expand Down
63 changes: 63 additions & 0 deletions leetcode/src/solutions/palindrome_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use super::Solution;

pub struct SolutionImpl;

impl Solution for SolutionImpl {
fn name(&self) -> String {
"Solution for Palindrome Number".into()
}
fn problem_id(&self) -> usize {
9
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [(121, true), (-121, false), (10, false)];

for (x, expect) in testcases {
let output = Self::is_palindrome(x);

if output != expect {
anyhow::bail!("test failed for x={x}, output={output}, expect={expect}");
}
}

Ok(())
}
fn benchmark(&self) -> anyhow::Result<usize> {
anyhow::bail!("TODO");
}
}

impl SolutionImpl {
pub fn is_palindrome(mut x: i32) -> bool {
if x < 0 {
false
} else if x < 10 {
true
} else {
let mut v = Vec::new();
while x > 0 {
let residue = x % 10;
x /= 10;

v.push(residue);
}

let mut start = 0;
let mut end = v.len() - 1;

while start < end {
if v[start] != v[end] {
return false;
}

start += 1;
end -= 1;
}

true
}
}
}
3 changes: 3 additions & 0 deletions runtime/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ pub fn register_all(handle: ContainerHandle) -> anyhow::Result<()> {
handle.register_problem(|_| problems::string_to_integer::ProblemImpl)?;
handle.register_solution(|_| solutions::string_to_integer::SolutionImpl)?;

handle.register_problem(|_| problems::palindrome_number::ProblemImpl)?;
handle.register_solution(|_| solutions::palindrome_number::SolutionImpl)?;

Ok(())
}

0 comments on commit 0c35892

Please sign in to comment.