Java ,accepted by using Long


  • 0
    L
    public class Solution {
        public int reverse(int x) {
            boolean isNegative = (x < 0); //负数标志
            long xL = (long)x *(isNegative ? -1 : 1);
            StringBuilder sb = new StringBuilder(String.valueOf(xL));
            String revX = sb.reverse().toString();
            long revXL = Long.valueOf(revX);
            if ((int)revXL != revXL) {
                return 0;
            }
            return (int)revXL * (isNegative ? -1 : 1);
        }
    }
    

Log in to reply
 

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