Palindrome Number -- solving notes, palindromenumber

Source: Internet
Author: User

Palindrome Number -- solving notes, palindromenumber

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

Click to show spoilers.

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 wocould you handle such case?

There is a more generic way of solving this problem.

Solution: Split the number of int types one by one, put all elements in a vector, and then determine whether the vector is symmetric between the left and right.

Note: Use push_back to add elements in a vector. Use the following table to retrieve the number of elements in a vector.

The Code is as follows:

Class Solution {public: bool isPalindrome (int x) {if (x <0) return false; vector <int> nums; while (x) {nums. push_back (x % 10); x/= 10;} int len = nums. size (); for (int I = 0; I <len/2; I ++) {if (nums [I]! = Nums [len-1-I]) % subscript pay attention to 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: 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.