Palindrome Number (input string)

Source: Internet
Author: User

Palindrome Number (input string)
Question:

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

Analysis:

This question is from leetcode. A text return string is a string that is the same for both the forward and reverse reads. For example, "level" or "noon" is a text return string. Of course, the return strings in integer form are similar. But the negative number is not a return string. Two ideas:

By definition, compare the beginning and end of a string in sequence until one or zero elements are separated (depending on whether the integer is an odd or even number of digits ). The advantage is that it can be quickly discovered when it is not a text string. For a return string, you must compare all digits. According to the characteristics of the input string, the numbers are the same in positive and negative order. The advantage is that it does not have to be determined by the number of digits. Code:
Class Solution {public: bool isPalindrome (int x) {vector

 
  
V; int I, j; if (x = 0) return true; while (x) {v. push_back (x % 10); x/= 10;} I = 0; j = v. size ()-1; while (I <j) {if (v. at (I ++ )! = V. at (j --) return false;} return true ;}};

 
Class Solution {public: bool isPalindrome (int x) {int y = 0; int t = x; if (t <0) // negative number is not the return false; while (t) {y = y * 10 + t % 10; t/= 10;} if (y ^ x) // judge whether the positive and negative orders are equal return false; return true ;}};


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.