Skip to content

Instantly share code, notes, and snippets.

@toliuweijing
Created August 4, 2013 21:56
Show Gist options
  • Save toliuweijing/6152115 to your computer and use it in GitHub Desktop.
Save toliuweijing/6152115 to your computer and use it in GitHub Desktop.
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