Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Created April 8, 2015 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/cf101d46903b71e01f18 to your computer and use it in GitHub Desktop.
Save walkingtospace/cf101d46903b71e01f18 to your computer and use it in GitHub Desktop.
palindrome-number
/*
https://leetcode.com/problems/palindrome-number/
exception case : negative, zero
filter x < 0 || X < 10 || x % 10
create int y by traversing x in reverse order
x = 12321, y = 0
y = 1=>12=>123=>1232=>12321
then if (x == y) true;
*/
class Solution {
public:
bool isPalindrome(int x) {
int origin = x;
int y = 0;
if(x < 0 || (x != 0 && x % 10 == 0)) {
return false;
}
if(x < 10) {
return true;
}
while(x > 0) {
int temp = x % 10;
x /= 10;
y = y*10 + temp;
}
if(origin == y) {
return true;
} else {
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment