"Leetcode" 007 Reverse Interger

Source: Internet
Author: User

Title: Leetcode 007 Reverse Interger

Test instructions: Reverses the number of an integer. Retains the positive and negative symbols.

Idea: First the integer into a string, then determine whether it is a negative number, or whether it contains ' + ', and then start from the end of the string to accumulate a new integer can be.

However, there are special cases in which the forward is in the int range, but it overflows after the reversal and therefore is subject to a specific contract.

The code is as follows:

1 classSolution {2  Public:3     intReverseintx) {4         intLen, flag =1, i =0;5         Long LongAns =0;6         strings =to_string (x);7 8Len =s.size ();9len--;Ten         if(S[i] = ='-') One         { AFlag =-1; -i++; -         } the         //else if (s[i] = = ' + ') i++; -  -          while(Len >=i) -         { +Ans *=Ten; -Ans + = s[len--]-'0'; +         } AAns *=Flag; at         //cout << ans << endl; -         //The return value is automatically changed according to the preset function type, and the Longlong is automatically handled by int overflow -         if(Ans > Int_max | | ans < int_min)return 0; -         returnans; -     } -};

"Leetcode" 007 Reverse Interger

Related Article

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.