Topic:
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie,-1)
If you is thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?
There is a more generic the solving this problem.
Test instructions
Determines whether an integer is a palindrome number. Do not take advantage of extra space.
Some hints: Negative numbers are not palindrome numbers; if you consider converting integers to strings, be aware of the limitations of space; If you try to transpose this integer, be aware of the overflow problem.
Algorithm Analysis:
Here the use of the topic "
Reverse Integer , transpose the given integer and then determine if the integer and the original integer are equal, and if equal is the palindrome number. Here are some special conditions to judge.
AC Code:
public class Solution {private int i;private int labelnum;private long finalnum;private int finalnnum;private int che ckresult;private int label; private int y; Private Boolean Blabel; public boolean ispalindrome (int x) {y=reverse (x);//Transpose the original integer if (y==0) {if (x==y) blabel=true;//before and after the transformation is 0 else blabel=false;//The original integer is not 0, transpose to 0, indicating an overflow} else if (x<0)//negative number is not a palindrome Blabel=false; else if (x==y) blabel=true; else Blabel=false; return Blabel; } public int reverse (int x) {String s1=integer.tostring (x); if (S1.charat (0) = = '-') {String s2= "-"; String finals2= "-"; String judges2= ""; Long num1=0l; For (I=s1.length () -1;i>=1;i--) S2+=s1.charat (i); For (I=1;i<s2.length (); i++) {if (S2.charat (i)! = ' 0 ') {labelnum=i; Break }} for (I=labelnum;i<s2.length (); i++) {Finals2+=s2.charat (i); }Label=checkstring (FINALS2);//Check for overflow problem, label=1 indicates no overflow, label=0 indicates overflow if (label==1) {Finalnum=integer.parseint (FINA LS2); } else {finalnum=0; }} else {String s2= ""; String finals2= ""; String judges2= ""; Long num1=0l; For (I=s1.length () -1;i>=0;i--) S2+=s1.charat (i); For (I=0;i<s2.length (); i++) {if (S2.charat (i)! = ' 0 ') {labelnum=i; Break }} for (I=labelnum;i<s2.length (); i++) {Finals2+=s2.charat (i); } label=checkstring (FINALS2);//Check for overflow problem, label=1 indicates no overflow; label=0 indicates overflow if (label==1) {Finalnum=integer.parseint (FINALS2); } else{finalnum=0; }} return (int) finalnum; } private int Checkstring (string string) {checkresult=1; /* Exception Condition 1: string is null */if (string = = null) checkresult=0; int length = String.Length (), offset = 0; /* Exception Condition 2: string length is 0 */if (length = = 0) checkresult=0; Boolean negative = string.CharAt (offset) = = '-'; /* Exception Case 3: String '-' * * * (negative && ++offset = = length) checkresult=0; int result = 0; char[] temp = String.tochararray (); while (offset < length) {char digit = temp[offset++]; if (digit <= ' 9 ' && digit >= ' 0 ') {int currentdigit = digit-' 0 '; /* Exception 4: Already equals INTEGER.MAX_VALUE/10, judging the last one to add: * If it is negative, the last one is 8 if it is positive, the last one is 7. * Int Range: four bytes, -2147483648~2147483647 */if (result = = INTEGER.MAX_VALUE/10) { if ((negative = = False && currentdigit > 7) | | (Negative && currentdigit > 8)) {checkresult=0; }/* Exception 5: Already greater than INTEGER.MAX_VALUE/10 * Whatever the last one will be more than Integer.max_value */} else if (Result > integer.maX_VALUE/10) {checkresult=0; } int next = result * + currentdigit; result = Next; }}return Checkresult; } }
code for someone's home:
public class Solution {public boolean ispalindrome (int x) { if (x<0) return false;//negative number is not a palindrome if (x==0) return true; The numerical value is flipped, and the palindrome is also equal to the original number int reversenum=0; int num=x; while (num>0) { int modnum=num%10; Consider the situation where rollover will overflow if ((REVERSENUM>INTEGER.MAX_VALUE/10) | | | ((REVERSENUM==INTEGER.MAX_VALUE/10) && (modnum>integer.max_value%10))) return false; Reversenum=reversenum*10+modnum; NUM=NUM/10; } if (reversenum==x) return true; else return false;} }
Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source
[Leetcode] [Java] Palindrome number