Skip to content

Commit 4d9437e

Browse files
committed
https://leetcode.com/problems/palindrome-number/
1 parent 76c8edd commit 4d9437e

File tree

2 files changed

+41
-46
lines changed

2 files changed

+41
-46
lines changed

EasyProblems/PalindromeNumber.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace LeetCode.EasyProblems
2+
{
3+
public class PalindromeNumber
4+
{
5+
public bool IsPalindrome(int x)
6+
{
7+
// Negative numbers and non-zero numbers ending with 0 can't be palindromes
8+
if (x < 0 || (x % 10 == 0 && x != 0))
9+
return false;
10+
11+
int reversedHalf = 0;
12+
// Extract and reverse the last half of the number
13+
while (x > reversedHalf)
14+
{
15+
reversedHalf = reversedHalf * 10 + x % 10;
16+
x /= 10;
17+
}
18+
19+
// For even digits: x == reversedHalf
20+
// For odd digits: x == reversedHalf/10 (middle digit is ignored)
21+
return x == reversedHalf || x == reversedHalf / 10;
22+
}
23+
24+
[Test(Description = "https://leetcode.com/problems/palindrome-number/")]
25+
[Category("Easy")]
26+
[Category("LeetCode")]
27+
[Category("Palindrome Number")]
28+
[TestCaseSource(nameof(Input))]
29+
public void Test1((bool Output, int Input) item)
30+
{
31+
var response = IsPalindrome(item.Input);
32+
Assert.That(item.Output, Is.EqualTo(response));
33+
}
34+
35+
public static IEnumerable<(bool Output, int Input)> Input =>
36+
new List<(bool Output, int Input)>()
37+
{
38+
(true, 121),
39+
};
40+
}
41+
}

Unfiltered/Random/PalindromeNumber.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)