Skip to content

Commit f6af21a

Browse files
add problem 9 add test function
1 parent a89b1a8 commit f6af21a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

C/1-50/9-Palindrome-Number.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)