Palindrome number (Palindrome string)

Source: Internet
Author: User

Title:

Determine whether an integer is a palindrome. Do this without extra space.

Analysis:

The topic originates from Leetcode. palindrome string is a literal and anti-read all the same strings, such as "level" or "noon" and so on is a palindrome string. Of course, the palindrome string is similar in integer form. But negative numbers are not palindrome strings. Two ways of thinking:

    1. By definition, the end of the string is compared to 1 or 0 elements in the middle (depending on whether the integer is an odd or even number of digits). The advantage is that when it is not a palindrome, it can be quickly discovered. If it is a palindrome, you must compare the entire number of digits.
    2. According to the characteristics of palindrome string, the inverse sequence, the number is the same. The advantage is not to be judged by the number of digits.
Code:
<span style= "Font-size:12px;font-weight:normal;" >class Solution {Public:bool ispalindrome (int x) {vector<int> v;int I, j;if (x = = 0) return True;while (x) {V.push_ Back (x%); x/= 10;} i = 0;j = V.size () -1;while (i < J) {if (v.at (i++)! = v.at (j--)) return false;} return true;}}; </span>
Class Solution {Public:bool ispalindrome (int x) {int y = 0;int T = x;if (T < 0)//negative number is not a palindrome return False;while (t) {y = y * Ten + t% 10;t/= 10;} if (y ^ x)//Determine if the inverse order is equal to return False;return true;}};


Palindrome number (Palindrome string)

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.