Solution in JAVA using module of the number


  • 0
    D
    class Solution {
        public int reverse(int x) {
            int number = Math.abs(x);
    
            String reversedNumber = "";
            if((x + "").startsWith("-"))
                reversedNumber += "-";
            
            while(number != 0){
                reversedNumber += number%10;
                number = number/10;
            }
            
            if(reversedNumber == "")
                return 0;
            try{
                int finalNum = Integer.parseInt(reversedNumber);
                return finalNum;
            }
            catch(Exception e){
                return 0;
            }
        }
    }
    

Log in to reply
 

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