Hello.. Thankyou for this video. Can you explain please why can't we use overflow condition like if (rev*10 > Integer.MAX_VALUE || rev*10 < Integer.MIN_VALUE)
so in the question it is given that it supports a signed 32-bit integer so assume your rev variable already has a value that is the maximum value for that range and then you perform rev*10 then the resulting value goes out of range and hence you cannot write rev*10 in place of MAX_VALUE/10 . I hope this helps :) ...
Hi .. It is giving 0 only when rev> max or min. .I have shown in the video ,when we don't consider that condition , it would give garbage value.. I think you missed the end part of video.. Please try the code from git repo.. Leetcode is accepting the code.. Thanks!
import java.util.*; class Main { public static void main (String [] args){ int nums=4567; System.out.print(reverseInt(nums)) ; } public static int reverseInt(int n) { int rev=0; while(n>0){ int Lastdigit=n%10; n=n/10; rev=rev*10+Lastdigit; } return rev; } }
Wow! You handled overflow condition greatly, Thank You!
mam please give the package name you havent displayed it correctly on screen without which integer isnt working
Great explanation. Thank you.
Amazing...keep up the good work. I like the way you break down the problem on white board but please slow it down a little bit when you code.
Sure
Great explanation, but it would be great if you use dark theme on leetcode
NICE SUPER EXCELLENT MOTIVATED
Hello.. Thankyou for this video.
Can you explain please why can't we use overflow condition like
if (rev*10 > Integer.MAX_VALUE || rev*10 < Integer.MIN_VALUE)
so in the question it is given that it supports a signed 32-bit integer so assume your rev variable already has a value that is the maximum value for that range and then you perform rev*10 then the resulting value goes out of range and hence you cannot write rev*10 in place of MAX_VALUE/10 . I hope this helps :) ...
Great Explanation .
Amazing explanation
Great explanation thank you😄👍
great Work !
Good explanation amrutha
Why you are dividing by 10 to check overflow condition
reversed * 10 > INT_MAX would mean performing the multiplication first which could already cause overflow
Really helpful
Hello mam if rev>max or min we need to get output as 0 then y it is returning garbage value .Leet code is not accepting this code...
Hi .. It is giving 0 only when rev> max or min. .I have shown in the video ,when we don't consider that condition , it would give garbage value.. I think you missed the end part of video.. Please try the code from git repo.. Leetcode is accepting the code.. Thanks!
If number is 6804 that is containing zero and we want reverse number 4086 then what we do? according to your solution it gives 486
Rev = 0
6804%10 = 4
Rev = rev*10+4
Rev=4
6804/10 = 680
680%10 = 0
Rev = rev *10+0
Rev = 40
680/10 = 68
68%10=8
Rev = rev*10+8
Rev = 408
68/10=6
6%10=6
Rev =rev*10+6
Rev=4086
6/10 =0
Loop ends
very well explained ! tysm
great explanation !!!
Thank you Di😊
Great !!
Amazing
anybody explain me why we do not use in overflow condition {reverse> (interger.max_value - digit)/10} ?
thankuu...😇
ye gys kaha se aa gya ?
didi
gys?? Sorry..didn't get your question..Can you be more specific?
import java.util.*;
class Main {
public static void main (String [] args){
int nums=4567;
System.out.print(reverseInt(nums)) ;
}
public static int reverseInt(int n) {
int rev=0;
while(n>0){
int Lastdigit=n%10;
n=n/10;
rev=rev*10+Lastdigit;
}
return rev;
}
}
class Solution {
public static int reverse(int x) {
int rev = 0;
while (x != 0) {
int lastdigit = x % 10;
// Check for overflow and underflow
if (rev > Integer.MAX_VALUE / 10 || (rev == Integer.MAX_VALUE / 10 && lastdigit > 7)) return 0;
if (rev < Integer.MIN_VALUE / 10 || (rev == Integer.MIN_VALUE / 10 && lastdigit < -8)) return 0;
rev = (rev * 10) + lastdigit;
x = x / 10;
}
return rev;
}
public static void main(String args[]) {
int x = 123;
System.out.println(reverse(x)); // Output: 321
}
}
This will run in runtime 1ms
Thank me later
great explanation!!