LeetCode 7: Reverse Integer - Interview Prep Ep 55

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


⭐ Support my channel and connect with me:

Solutions explained:
Solution #1:
We could use a bigger type (e.g. Long) to handle integer overflow case, but that's not acceptable based on the problem requirements.

Solution #2:
We can use a temp variable to hold the newly computed result and compare it with the previous result, if overflow happens, these two numbers won't equal, then we'll just return zero.

// TOOLS THAT I USE:

// MY FAVORITE BOOKS:

My ENTIRE Programming Equipment and Computer Science Bookshelf:

And make sure you subscribe to my channel!

Your comments/thoughts/questions/advice will be greatly appreciated!

#softwareengineering #leetcode #algorithms #coding #interview #SDE #SWE #SiliconValley #programming #datastructures
Рекомендации по теме
Комментарии
Автор

This was an elegant solution! thanks for explaining it so well !

sstee
Автор

very good explaination brother. loved it.

entertainmentadda
Автор

you're amazing! someone finally explained what the variable mean

TehShox
Автор

Thank you so much. Best and most detailed explanation!

TheAbrykos
Автор

Thank you so much for the detailed explanation.

suprajaaravind
Автор

It was very good and easy to understand solution, Thanks!

akhilpatil
Автор

This was indeed an elegant solution but it is no longer accepted on LC.
result*10 > this part may exceed the upper bound of integer.

int res = 0;
while(x)
{
int mod = x%10;
int temp = mod + res*10;
if((temp -mod)/10 != res)
return 0;

res = temp;
x = x/10;
}
return res;
Error -
runtime error: signed integer overflow: 964632435 * 10 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:18:33

bestsaurabh
Автор

I got this runtime error
Line 8: Char 33: runtime error: signed integer overflow: 964632435 * 10 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:17:33

cppnewbie
Автор

Thanks a lot Steve! Btw, May I ask what the runtime is?

yitingg
Автор

In that case Where we used long data type for result, there the code also return 0, If the input is between 2463847412 and 7463847412(result should be 2147483642 to 2147483647 but it is 0) even though these are less than the MAX VALUE (2147483647) of int. Can you explain why??

anandyadav_
Автор

847. Shortest Path Visiting All Nodes (Leetcode Hard) this problem please

ijaz
Автор

This solution will not pass if you are using cpp

amanjain