Leetcode Reverse Integer

Source: Internet
Author: User

Reverse Integer Total accepted:61132 total submissions:219035 My submissions Question Solution
Reverse digits of an integer.

EXAMPLE1:X = 123, return 321
example2:x = -123, return-321

Click to show spoilers.

Has a thought about this?


Here is some good questions to ask before coding. Bonus points for if you have already thought through this!

If the last digit are 0, what should the output being? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow?

Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?

For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

Test instructions: Reverse the number, but be aware of overflow situations. There is a 100 reverse order, and for overflow cases, return 0,100 is 1 instead of 001.


First solution is given:
Since it's a reversal, the simplest thing to do is to think of numbers as strings and then reverse them. The minus sign is specially handled. Then use SSCANF to convert the string into numbers.

For the overflow problem, be able to think so, if the input is x, if the reverse order overflow, x length is at least 10 bits. In addition If the string after the reverse order is s1,sscanf after the number is Y, then this y corresponding string S2 will and S1 are not equal!

The code is as follows:

classSolution { Public:intReverseintx) {intresult=0;Chars[ A];Charr[ A];sprintf(S,"%d", x);intI=0;if(s[0]=='-') {r[0]='-';        i++; } r[strlen(s)] =' + ';intj=strlen(s)-1; while(i<strlen(s)) {r[i]=s[j];            j--;        i++; }sscanf(R,"%d", &result);if(strlen(s) >=Ten)//Overflow possible{sprintf(S,"%d", result);if(strcmp(r,s)) result=0; }returnResult }};

1032/1032 Test cases passed.
status:accepted
Runtime:16 ms

Another solution:
Well, actually, that's the simplest way. By repeating the number of digits on the front shift is good, if the overflow directly returns 0!

 class solution {   Public:int Reverse(intx) {constintMax =0x7fffffff;//int Maximum ValueConstintMin =0x80000000;//int Minimum Value        Long Long sum=0; while(X! =0)          {inttemp = x%Ten;sum=sum*Ten+ temp;if(sum> Max | |sum< min)//Overflow handling                return 0; x = x/Ten; }return sum; }  };

1032/1032 Test cases passed.
status:accepted
Runtime:14 ms

Leetcode Reverse Integer

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.