File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Determine whether an integer is a palindrome. Do this without extra space.
3+ * Created by supercoderhawk on 2017/7/24.
4+ */
5+ #include <stdio.h>
6+ #include <stdlib.h>
7+ #include "../main.h"
8+
9+ bool isPalindrome (int x ) {
10+ if (x == 0 )
11+ return true;
12+ if (x < 0 )
13+ return false;
14+ int length = 0 , num = x ;
15+ int * digits = (int * )malloc (sizeof (int )* 10 );
16+
17+ while (num != 0 )
18+ {
19+ digits [length ++ ] = num %10 ;
20+ num /=10 ;
21+ }
22+ if (length == 1 )
23+ return true;
24+
25+ for (int i = 0 ; i < length /2 ;i ++ )
26+ {
27+ if (digits [i ] != digits [length - 1 - i ])
28+ return false;
29+ }
30+ return true;
31+ }
32+
33+ void testIsPalindrome ()
34+ {
35+ printf ("%d\n" , isPalindrome (54245 ));
36+ printf ("%d\n" , isPalindrome (54246 ));
37+ printf ("%d\n" , isPalindrome (112211 ));
38+ printf ("%d\n" , isPalindrome (1000000001 ));
39+ }
You can’t perform that action at this time.
0 commit comments