-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add problem polindrome number * add solution polindrome number * impl solution polindrome number
- Loading branch information
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters