简体   繁体   中英

Reversing an integer in java (question about the method)

The problem statement is kind of basic - if the input is a 32 bit signed integer output reversed integer, else output 0.

Here is the code I came up with

public class Solution {
    public int reverse(int A) {
        if(A>=2143483647 || A<-2143483647)
        return 0;
        if(A>=0)
            return Integer.parseInt((new StringBuilder(String.valueOf(A))).reverse().toString());
        else
            return -1*Integer.parseInt((new StringBuilder(String.valueOf(-1*A))).reverse().toString());
    }
}

The solution is not accepted. The problem is in my code or the test cases?

Assuming the input is int32, here is a possible approach, including checking for overflow.

public class Solution {
    public int reverse(int A) {
        //if(A < Integer.MIN_VALUE || A > Integer.MAX_VALUE) return 0;
        boolean neg = A < 0;
        A = Math.abs(A);
        long ret = 0;
        while(A != 0){
            ret = ret*10 + A%10;
            A = A/10;
        }
        if(ret > Integer.MAX_VALUE) return 0;
        return neg ? -(int)ret : (int)ret;
    }
}

Be mindful to change int to long if the input is bigger.

The accepted answer is close but misses some edge cases. Here is an answer based on the accepted answer, but hopefully correctly handles all cases.

public static int reverse(int A) {

    long aLong = Math.abs((long)A);
    long ret = 0;
    while (aLong != 0) {
        ret = ret * 10 + aLong % 10;
        aLong = aLong / 10;
    }
    if (A < 0) {
        ret = -ret;
    }

    if ((ret < Integer.MIN_VALUE) || (ret > Integer.MAX_VALUE)) {
        return 0;
    } else {
        return (int) ret;
    }

you can try reversing into a long parameter first (this guarantees that it'll not overflow), then do the checking afterwards.

public int reverse(int A) {
        long reversed; 
        if(A>=0)
            reversed=  reverseString(A);
        else
            reversed -1*  reverseString(-A) ;

        //we do the checking only after we have done the reverse.
        if(reversed > Integer.Max_VALUE || reversed < Integer.MIN_VALUE)
            return 0;
        else 
            return (int) reversed; //we do a down cast here.
}

public long reverseString(int A){
    StringBuilder sb = new StringBuilder(""+A).reverse(); 
    String s = sb.reverse().toString(); 
    return Long.parseLong(s);
}

This code will take care of positive and negative both numbers.After reversal if result is greater than Integer.MAX_VALUE then it will return zero.

  public static int reverse(int x) {

     boolean positive = true;
     if(x < 0) {
         positive =  false;
         x = x * -1;
     }

     long result = 0;
     while(x > 0) {
         result = (result * 10) + (x % 10);
         x = x / 10;
     }

     if(result > Integer.MAX_VALUE) {
         return 0;
     }

     return positive ? (int)result : (int)(result * -1);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM