Reverse the digits of an Integer Reverse digits of an integer.

Example1:

x = 123,

return 321
Example2:

x = -123,

return -321

Return 0 if the result overflows and does not fit in a 32 bit signed integer

This is my approach:

public class Solution {
    public int reverse(int A) {

        String ans = " ";
        String finalAns = " ";
        int range = 2147483647;
        if( (A &gt; 0) &amp;&amp; (A &lt;= range))
            {
                ans = String.valueOf(A);
                return (Integer.parseInt(new StringBuilder(ans).reverse().toString()));
            }
        if( (Math.abs(A) &gt; range))
            return 0;

        else if ( (A &gt; - (range + 1)) &amp;&amp; (A &lt; 0) )
            {
                ans = String.valueOf(Math.abs(A));

                if( Math.abs(A) &gt; range)
                    return 0;

                finalAns =  (new StringBuilder(ans).reverse().toString());

                double d = Double.parseDouble(finalAns);
                if( d  == (int)d )
                    return Integer.parseInt( "-" + finalAns);
                else
                    return 0;
            }
        else
            return 0;
    }
}


I have following questions regarding my code:

1) Is there a better approach to solve this question?

2) Are there any Java coding conventions that I have violated gravely?

3) Am I doing redundant steps?

Reference
If I were presented with that solution as an answer to an interview question, I would definitely question your judgement and taste, even if the solution worked correctly — which it doesn't.

The value for range, 2147483647, is a magic number, better written as Integer.MAX_VALUE.  However, even if (A &gt; 0) &amp;&amp; (A &lt;= range) is true, the reversed result might not fit in an int, in which case Integer.parseInt(…) would throw a NumberFormatException.

I have previously advised you to avoid using floating-point operations when dealing with integer arithmetic.  If your goal is to handle int overflow, then use long.

In any case, using StringBuilder.reverse() is, in my opinion, a bad idea.  CPUs are very efficient at arithmetic, but less adept at handling strings.  Both String.valueOf(int) and Integer.parseInt(String) do a lot of work behind the scenes.  Though the inefficiency is quite tolerable for all practical purposes, I would consider the solution to have missed the mark, if judged as an interview solution.

I'd be happier with a purely arithmetic solution like this, which should be far more efficient, and that demonstrates your ability to write a loop:

public int reverse(int a) {
    long reversed = 0;
    while (a != 0) {
        reversed = 10 * reversed + (a % 10);
        a /= 10;
    }
    return (reversed == (int)reversed) ? (int)reversed
                                       : 0;    // int overflow detected
}


Note that the overflow detection is also much simpler.  There are no special cases — we simply check whether downcasting from long to int can be safely done.  Also note that (a % 10) produces a negative result when a is negative, so that no special handling is needed for negative numbers either.