File tree Expand file tree Collapse file tree 2 files changed +41
-46
lines changed Expand file tree Collapse file tree 2 files changed +41
-46
lines changed Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments