Leetcode [7] (Java): Reverse Integer Tags: math

Source: Internet
Author: User

title : Reverse Integer

Difficulty : Easy

topic content :

Given a 32-bit signed integer, reverse digits of an integer.

Note:
Assume we is dealing with a environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.

translation : Given a 32-bit signed integer, the inverse number of an integer.

Attention:

Let's say we're dealing with an integer that can hold only 32 bits. For the purpose of this problem, the function returns 0 when the integer overflow is reversed.

Example 1:

Input:123output:  321

Example 2:

Input: -123output:-321

Example 3:

Input:120output:21

My idea : Use list to take the number from the low to the high, and the first of the list to record the symbol of this integer. At the end of the output, a Boolean is used to determine if the front is all zeros, and then the zero is skipped.

My Code :

1      Public intReverseintx) {2list<integer> ans =NewArraylist<integer>();3         if(X < 0) {4Ans.add (0);5x =-x;6}Else if(X > 0) {7Ans.add (1);8}Else {9             return0;Ten         } One          while(x! = 0) { AAns.add (x%10); -x = X/10; -         } the         inty = 0; -         BooleanTag =false;  -          for(inti = 1;i < Ans.size (); i++) { -             if(Tag = =false&& Ans.get (i) = = 0) { +                 Continue; -}Else { +Tag =true; A             } atY + =Ans.get (i); -             if(I < Ans.size ()-1) Y *= 10; -         } -y = ans.get (0) = = 1? Y:-y; -         returny; -}

Results:1027/1032 Test Cases passed.

Input:1534236469 Output:1056389759 expected:0 This means that you need to decide whether to overflow after the reversal. I have not thought for a long while, because after the reversal of a person multiplied by 10 after the direct overflow and then become another value, not good comparison whether cross-border .... Forgive me for being such a stupid brain. Programming Problems: 1, the Boolean type is again assigned value when accidentally used = =, the result Leetcode error: not a Statement

Answer :

1      Public intReverseintx) {2         intresult = 0;3          while(x! = 0)4         {5             inttail = x 10;6             intNewresult = result * 10 +tail;7             if((newresult-tail)/10! =result)8{return0; }9result =Newresult;Tenx = X/10; One         } A         returnresult; -}

There is no justice .... 13 lines will be done?!

Answer Ideas :

1, because the negative number for the remainder and divided by 10 or negative , so do not need to record symbols.

2, because if the current reversal of the first number is zero, multiplied by 10 or 0 , so do not need to record with the list.

3, because multiplying by 10 plus a value may overflow cannot be compared with the max value, then the (max-tail)/10 is compared with the current value! Hey, I'm so stupid, I can.

4, can be directly with the new value to reverse the old value to compare , changed the description, so that you do not have to tail for positive and negative judgment (when the tail is negative should be (Min-tail)/10 > result should be (Max-tail)/10 < Result)

Leetcode [7] (Java): Reverse Integer Tags: math

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.