Created
August 4, 2013 21:56
-
-
Save toliuweijing/6152115 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPalindrome(int x) { // 123 | |
if (x < 0) return false; | |
long long num = x; | |
// find the high digit | |
int h = 11; | |
while (h >= 0 && digit(h, num) == 0) h--; | |
// matching | |
int l = 0; | |
while (l < h) { | |
if (digit(h, num) != digit(l, num)) return false; | |
l++; h--; | |
} | |
return true; | |
} | |
// idx starting from 0 | |
long long digit(int idx, long long num) { | |
long long base = (long long) pow(10, idx); | |
long long cut = (long long) pow(10, idx+1); | |
return (num%cut) / base; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment