[LeetCode] [Java] Palindrome Number
Question:
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Cocould negative integers be palindromes? (Ie,-1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You cocould also try reversing an integer. However, if you have solved the problem Reverse Integer, you know that the reversed integer might overflow. How can you handle such case?
There is a more generic way of solving this problem.
Question:
Determines whether an integer is a return value. Do not use extra space.
Some tips: The negative number is not the return number. If you want to convert the integer into a string, pay attention to the space limit. If you want to transpose this integer, pay attention to the overflow problem.
Algorithm analysis:
Here we use the question 《
Reverse Integer, transpose the given Integer, and then judge whether the converted Integer is equal to the original Integer. Pay attention to the judgment of some special conditions.
AC code:
Public class Solution {private int I; private int labelnum; private long finalnum; private int finalnnum; private int checkresult; 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; // The values before and after conversion are both 0 else blabel = false; // The original integer is not 0, and the value changes to 0 after conversion, indicating overflow} else if (x <0) // The negative number is not the return value 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
= 0; I --) s2 + = s1.charAt (I); for (I = 0; I
= '0') {int currentDigit = digit-'0';/** exception situation 4: It is already equal to Integer. MAX_VALUE/10: determines the last bit to be added: * if it is a negative number, the last bit is 8. If it is a positive number, the last bit is 7 * Int: four bytes,-2147483648 ~ 2147483647 */if (result = Integer. MAX_VALUE/10) {if (negative = false & currentDigit> 7) | (negative & currentDigit> 8) {checkresult = 0 ;} /** exception 5: The value is greater than Integer. MAX_VALUE/10 * No matter what the last digit is, it will exceed Integer. MAX_VALUE */} else if (result> Integer. MAX_VALUE/10) {checkresult = 0;} int next = result * 10 + currentDigit; result = next ;}} return checkresult ;}}
Others' code:
Public class Solution {public boolean isPalindrome (int x) {if (x <0) return false; // a negative number is not a return if (x = 0) return true; // flip the numeric value. After the return flip, it is equal to the original number int reverseNum = 0; int num = x; while (num> 0) {int modnum = num % 10; // 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 ;}}