Most Straightforward way to AC this problem, Java


  • 0
    H
    import java.math.*;
    public class Solution {
        public int reverse(int x) {
            boolean positive = true;
            if (x == Integer.MIN_VALUE) return 0;
            if (x < 0) {
                positive = false;
                x *= -1;
            }
            StringBuilder x_s = new StringBuilder(String.valueOf(x));
            x_s.reverse();
            BigInteger bi = new BigInteger(x_s.toString());
            BigInteger max = new BigInteger(String.valueOf(Integer.MAX_VALUE));
            if (bi.compareTo(max) > 0) return 0;
            return positive ? Integer.valueOf(x_s.toString()) : -1 * Integer.valueOf(x_s.toString());
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.