Created
April 8, 2015 17:42
-
-
Save walkingtospace/cf101d46903b71e01f18 to your computer and use it in GitHub Desktop.
palindrome-number
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
/* | |
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