Leetcode:palindrome number-Palindrome

Source: Internet
Author: User

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;&LT;&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;&LT;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.