http://articles.leetcode.com/2012/01/palindrome-number.html
Determine whether an integer is a palindrome. Do this without extra space. Assume that a negative integer is not a palindrome.
public boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0)) return false; int rev = 0; while (x>rev){ rev = rev*10 + x%10; x = x/10; } return (x==rev || x==rev/10); }
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/
可以首先排除能被10整除的数,这样的数是不可能为回文数的。
http://codercareer.blogspot.com/2011/11/no-23-palindrome-numbers.html
Solution 1: Convert a Number into a String
This should be preferred in real application: as its performance would be much better. JDK's Interget.toString is highly optimized.
Solution 2: Compose a Reversed Number
Recursive Solution:
http://yucoding.blogspot.com/2013/04/leetcode-question-62-palindrome-number.html
http://www.geeksforgeeks.org/check-if-a-number-is-palindrome/
http://bylijinnan.iteye.com/blog/1346521
Read full article from LeetCode - Palindrome Number | Darren's Blog
Determine whether an integer is a palindrome. Do this without extra space. Assume that a negative integer is not a palindrome.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
https://leetcode.com/discuss/23563/line-accepted-java-code-without-the-need-handling-overflowpublic boolean isPalindrome(int x) { if (x<0 || (x!=0 && x%10==0)) return false; int rev = 0; while (x>rev){ rev = rev*10 + x%10; x = x/10; } return (x==rev || x==rev/10); }
Overflow does not guarantee to be negative. Think about 2147483647 *3 is also positive.
可以首先排除能被10整除的数,这样的数是不可能为回文数的。
public boolean isPalindrome(int x) {
if (x < 0) // Negative numbers are assumed to be non-palindromic
return false;
// The smallest integer that has the same number of digits with x
// Used to get the leftmost digit
int oneZeros = 1; // change variable name to div: readability
while (x / oneZeros >= 10)
oneZeros *= 10;
// Compare the digits at both ends
while (x != 0) {
int leftmost = x / oneZeros;
int rightmost = x % 10;
if (leftmost != rightmost)
return false;
x = (x % oneZeros) / 10; // Chop the leftmost and rightmost digits
oneZeros /= 100; // Update oneZeros as two digits are chopped
}
return true;
}
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/
Problems related with numbers are frequently solved by / and %. No need of extra space is required. This problem is similar with the Reverse Integer problem.
直接按照PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。
public boolean isPalindrome(int x) { //negative numbers are not palindrome if (x < 0) return false; // initialize how many zeros int div = 1; while (x / div >= 10) { div *= 10; } while (x != 0) { int left = x / div; int right = x % 10; if (left != right) return false; x = (x % div) / 10; div /= 100; } return true; }
http://codercareer.blogspot.com/2011/11/no-23-palindrome-numbers.html
Solution 1: Convert a Number into a String
This should be preferred in real application: as its performance would be much better. JDK's Interget.toString is highly optimized.
Solution 2: Compose a Reversed Number
A simple method for this problem is to first reverse digits of num, then compare the reverse of num with num. If both are same, then return true, else false.
==> it may overflow
// without consider about overflow
Use bigger data type
bool IsPalindrome_solution2(unsigned int number)
{
int reversed = 0;
int copy = number;
while(number != 0)
{
reversed = reversed * 10 + number % 10;
number /= 10;
}
return (reversed == copy);
}
考虑越界溢出问题。遇见这种处理Number和Integer的问题,首要考虑的就是会不会溢出越界。然后再考虑下负数,0。还有就是要心里熟悉\的结果和%的结果。
http://yucoding.blogspot.com/2013/04/leetcode-question-62-palindrome-number.html
bool
check(
int
x,
int
&y){
if
(x==0) {
return
true
;}
if
(check(x/10,y)){
if
(x%10==y%10){
y=y/10;
return
true
;
}
}
return
false
;
}
bool
isPalindrome(
int
x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if
(x<0){
return
false
;}
return
check(x,x);
}
http://www.geeksforgeeks.org/check-if-a-number-is-palindrome/
int
isPal(
int
num)
{
// If num is negative, make it positive
if
(num < 0)
num = -num;
// Create a separate copy of num, so that modifications made
// to address dupNum don't change the input number.
int
*dupNum =
new
int
(num);
// *dupNum = num
return
isPalUtil(num, dupNum);
}
// A recursive function to find out whether num is palindrome
// or not. Initially, dupNum contains address of a copy of num.
bool
isPalUtil(
int
num,
int
* dupNum)
{
// Base case (needed for recursion termination): This statement
// mainly compares the first digit with the last digit
if
(oneDigit(num))
return
(num == (*dupNum) % 10);
// This is the key line in this method. Note that all recursive
// calls have a separate copy of num, but they all share same copy
// of *dupNum. We divide num while moving up the recursion tree
if
(!isPalUtil(num/10, dupNum))
return
false
;
// The following statements are executed when we move up the
// recursion call tree
*dupNum /= 10;
// At this point, if num%10 contains i'th digit from beiginning,
// then (*dupNum)%10 contains i'th digit from end
return
(num % 10 == (*dupNum) % 10);
}
- //generic solution, may overflow
- public boolean isPalindromeInt(int x){
- if(x<0)return false;
- int x2=x;
- int y=0;
- while(x>0){
- y*=10;
- y+=x%10;
- x/=10;
- }
- return x2==y;
- }
bool isPalindrome(int x) {
long reverse = 0;
long num = abs(x);
while(x != 0){
reverse *= 10;
reverse += x % 10;
x /= 10;
}
return reverse == num;
}
The basic idea is to reverse
x
.
However, we need to handle two issues. First of all, what if reverse number overflows? We use
http://liangjiabin.com/blog/2015/03/leetcode-palindrome-number-in-python.htmllong
to solve. Secondly, negative number doesn't have palindrome. So we make num = abs(x).Read full article from LeetCode - Palindrome Number | Darren's Blog