OJ Practice 3--t9

Source: Internet
Author: User

Palindrome number

Determines whether a positive integer is a palindrome. No extra space is allowed.

Analysis

The topic suggests that if you want to convert integers to string type, do not meet the topic does not occupy additional space, in addition

You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?

So we can only use the mathematical calculation,/and% to calculate each bit.

classSolution { Public:    BOOLIspalindrome (intx) {if(x<0)            return false; int Base=1; intLeft ; intj=0, i=0;  while(x/Base>=Ten){            Base*=Ten; J++; } Left=Base;  while(x/left==x%Ten&&x>Ten) {i++; X%=Left ; X/=Ten; if(left> -) Left/= -; }        if(x<Ten&&i>=j/2)            return true; Else if(x==0)            return true; Else            return false; }};

"Summary" 124ms

At first, I couldn't imagine how to get the highest bit, each time divided by 10 too cumbersome to see the other people's request base to get enlightenment.

OJ Practice 3--t9

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.