Skip to content

Bug Report for reverse-integer #5154

@0pain01

Description

@0pain01

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)

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions