1. Title
Palindrome Number (palindrome)
2. Address of the topic
Https://leetcode.com/problems/palindrome-number
3. Topic content
English: Determine whether an integer is a palindrome. Do this without extra space.
English: Confirm whether an integer is a palindrome number
4. Method of solving Problems 1
After the number is flipped to determine whether the original number is equal, you can refer to Leetcode 7th (Reverse Integer) problem-solving ideas. The Java code is as follows:
/** * function Description:leetcode 9 - palindrome number * developer:tsybius * Development Date: September 24, 2015 */public class solution { / ** * determine if a number is a palindrome * @param x number * @return true: Yes, false: No */ public boolean ispalindrome (int x) { //negative number cannot be a palindrome if (x < 0) { return false; } //reversal number observation is equal if (X&nbsP;== reverse (x)) { return true; } else { return false; } } /** * Invert digital * @param x reversed Digital * @ return Inverted number */ public int reverse (int &NBSP;X) { long result = 0; while (x!=0) { result = result * 10 + x % 10; x /= 10; } return (result > integer.max_value | | result < integer.min_value) ? 0 : (int) result; } }
5. Method of solving Problems 2
Another method is to convert a number to a string and then reverse the string with the StringBuilder class to determine whether the two strings are equal. The Java code is as follows:
/** * function Description:leetcode 9 - palindrome number * developer:tsybius * Development Date: September 24, 2015 */public class solution { / ** * determine if a number is a palindrome * @param x number * @return true: Yes, false: No */ public boolean ispalindrome (int x) { if (x < 0) return false; if (X&NBSP;<&NBSP;10) return true; string str1 = string.valueof (x); String str2 = (New stringbuilder (STR1)). Reverse (). toString (); if (Str1.equals (str2)) { return true; } else { return false; } }}
6. Method of solving Problems 3
Another way is to convert directly to a string, and then compare the character of the string symmetric position separately. The Java code is as follows:
/** * function Description:leetcode 9 - palindrome number * developer:tsybius * Development Date: September 24, 2015 */public class solution { / ** * determine if a number is a palindrome * @param x number * @return true: Yes, false: No */ public boolean ispalindrome (int x) { if (x < 0) return false; if (X&NBSP;<&NBSP;10) return true; string str = string.valueof (x); for (Int i = 0; i < str.length () / 2; i++) { if (Str.charat (i) != Str.charat (Str.length () - i - 1)) { return false; } } return true; }}
END
Leetcode:palindrome number-Palindrome