LeetCode Reverse Integer Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Integer.MAX_VALUE is equal 2147483647. Integer.MIN_VALUE is equal -2147483648. Last digits are 7 and 8. This is the reason why we check pop>7 and pop < -8 conditions

wayfarer
Автор

Similar solution, easy to understand, checking the boundary condition before so we don't have to use pop

int rev =0;
while(x!=0)
{
if(rev<Integer.MIN_VALUE/10 || rev>Integer.MAX_VALUE/10)
return 0;
rev = rev * 10 + x%10;
x/=10;
}
return rev;

pragyadhawan
Автор

Good Job, seeing your mistakes is good, makes us see how you debug

rayprusia
Автор

i didn't get it why there is reversed = (reversed * 10) + pop; ???

aislofi
Автор

Thanks for it, I convert it to Python format.
```
class Solution:
def reverse(self, x: int) -> int:
reversed = 0
sign = -1 if x < 0 else 1
x = abs(x)

while x > 0:
pop = x % 10
x //= 10

reversed = reversed * 10 + pop

if reversed > -2**31 and reversed < 2**31 - 1:
return reversed * sign

return 0
```

ZhouHaibo
Автор

Excellent job! You really broke this down for a novice like myself.

CaseTechnologies
Автор

Having watched a video where it was said when learning a new language it's good trying interview questions. So I did this question in Dart - and it's good to now refer to your video to see similar or differences in the way you approached.

lifeofamobiledeveloper
Автор

In 3:40 and 4:12 why do we have conditions for pop > 7 and pop<=8

Fahodinho
Автор

why the while(x !=0) loop not a infinite loop?

masong
Автор

Why does Integer.MAX_VALUE has to be divided by 10?

matthewstraughn
Автор

Not sure how fancy their compiler is and if it optimizes, but if you take those MAX/MIN calculations out of the loop and store them in a variable then you are trading 4N divisions for just 2 total. A real compiler would probably optimize this though, and as you said there is a Wifi bug anyway. 😁

Gideon_Judges
Автор

Hey Nick,
can u explain y we are using pop varibale inside the if conditions

thesoftwareengineer
Автор

Can someone please elaborate why we divide Max and Min by 10..None of the comments helped me out plss 🙏

pratikawate
Автор

Hey Nick, why Min and Max values are divided by 10 and why do you check for pop being >7 or <-8????

_-
Автор

For those who do not understand the overflow check... it's not necessary :)



We can do:


int reversedCopy = reversed;


reversed = reversed * 10 + pop;

if ( (reversed - pop) / 10 != reversedCopy) {
return 0
}

darod
Автор

why Pop > 7 or pop < -8 in boundary checking?

jollymail
Автор

I am having trouble understanding what reversed = (reversed * 10) + pop; does. What integer does reversed store? in other words, what is being multiplied by 10?

jayhun
Автор

Does anyone know how to do this but with a string as an input?

dontgoogleme-yebt
Автор

Help me understand the logic behind the pop>7 and pop < -8 conditions please.

adityaranjalker
Автор

Hi, can someone explain what is meant by 32 bit signed integer?

okey