forked from Dungeonlx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalin_num.cpp
46 lines (38 loc) · 801 Bytes
/
palin_num.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
unsigned int reverse(unsigned int num)
{
unsigned int rev = 0;
while (num)
{
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}
bool isPalindrome(int x)
{
if (x < 0) return false;
int div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
// remove l and r
// l -------------- r
x = (x % div) / 10;
// remove l and r 2 digits
div /= 100;
}
return true;
}
int main(int argc, char* argv[])
{
unsigned int num = 616;
printf("%d\n", argv[1] ? reverse(atoi(argv[1])) : reverse(num));
printf("%d\n", argv[1] ? isPalindrome(atoi(argv[1])) : isPalindrome(num));
return 0;
}