We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9d46204 commit 6003ba9Copy full SHA for 6003ba9
palindrome_number.cpp
@@ -0,0 +1,33 @@
1
+class Solution {
2
+public:
3
+ bool isPalindrome(int x) {
4
+ if (x < 0)
5
+ return false;
6
+ if (x == 0)
7
+ return true;
8
+
9
+ int k = getNumDigit(x);
10
11
+ for (int i = 1, j = k; i < j; ++i, --j)
12
+ {
13
+ if (getKthNum(x, i) != getKthNum(x, j))
14
15
+ }
16
17
18
19
+ int getNumDigit(int x)
20
21
+ int k = 1;
22
+ while (x /= 10) ++k;
23
+ return k;
24
25
+ int getKthNum(int x, int k)
26
27
+ if (k == 1)
28
+ return x % 10;
29
30
+ long mask = pow(10, k);
31
+ return (x % mask - x % (mask / 10)) / (mask / 10);
32
33
+};
0 commit comments