Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palindrome number #17

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}