Reverse Integer
0 0
Read Time:53 Second

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Example 1:Input: x = 123 Output: 321

Example 2:Input: x = -123 Output: -321

Example 3:Input: x = 120 Output: 21

Solution 1

class Solution:
    def reverse(self, x: int) -> int:
        it_max= 2**31 - 1
        it_min= -2**31

        rev=0
        sign=1
        if x<0: 
            sign=-1 
        else:
            sign=1 
        x=abs(x)
        rev= int(str(x)[::-1])*sign        
 
        if rev >it_max:
            return 0
        if rev <it_min:
            return 0

        return rev

Solution 2

class Solution:
    def reverse(self, x: int) -> int:
        it_max= 2**31 - 1
        it_min= -2**31

        rev=0
        sign=1
        if x<0: 
            sign=-1 
        else:
            sign=1 
        x=abs(x)
        
        while x>0:
            z= x%10
            rev= rev*10+z
            x=x//10
        
        if rev >it_max:
            return 0
        if rev <it_min:
            return 0

        return rev*sign
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

0 thoughts on “Reverse Integer

  1. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.

  2. Hi my family member! I want to say that this post is awesome, nice written and come with approximately all significant infos. I would like to peer extra posts like this.

  3. Somebody essentially lend a hand to make significantly posts I might state That is the very first time I frequented your web page and up to now I surprised with the research you made to create this particular put up amazing Excellent job

  4. I have been surfing online more than 3 hours today yet I never found any interesting article like yours It is pretty worth enough for me In my opinion if all web owners and bloggers made good content as you did the web will be much more useful than ever before

  5. Thanks I have recently been looking for info about this subject for a while and yours is the greatest I have discovered so far However what in regards to the bottom line Are you certain in regards to the supply

  6. hiI like your writing so much share we be in contact more approximately your article on AOL I need a specialist in this area to resolve my problem Maybe that is you Looking ahead to see you

  7. I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *