-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Open
Description
Bug Report for https://neetcode.io/problems/reverse-integer
I am using absolute function of math library to remove the negative sign but its not working for the compiler.
for input :
x=-2147483648
my code:
public int reverse(int x) {
int org = x;
x = Math.abs(x);
long res = Long.parseLong(new StringBuilder(
String.valueOf(x)).reverse().toString()
);
if (org < 0) {
res *= -1;
}
if (res < -(1 << 31) || res > (1 << 31) - 1) {
return 0;
}
return (int)res;
}
error is coming this :
java.lang.NumberFormatException: For input string: "8463847412-"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Long.parseLong(Long.java:711)
at java.base/java.lang.Long.parseLong(Long.java:836)
at Main$Solution.reverse(Main.java:5)
at Main.main(Main.java:27)

Metadata
Metadata
Assignees
Labels
No labels