Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 14, 2015 13:39
Show Gist options
  • Save guolinaileen/5095316 to your computer and use it in GitHub Desktop.
Save guolinaileen/5095316 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<0) return false;
int counter=1;
while(x/counter>=10)
{
counter*=10;
}
while(x!=0)
{
if(x/counter!=x%10) return false;
x=(x%counter)/10;
counter/=100;
}
return true;
}
};
public class Solution {
public boolean isPalindrome(int x) {
// Start typing your Java solution below
// DO NOT write main() function
if(x<0) return false;
int counter=1;
while(x/counter>=10)
{
counter*=10;
}
while(x!=0)
{
int left=x/counter;
int right=x%10;
if(left!=right) return false;
x=(x-left*counter-right)/10;
counter/=100;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment