Reverse Integer - Think Simple


  • 0
    R

    I have tried simple code that passed all the test cases, have a look and comment if anyone thinks there is a better approach to handle it;).....
    '''
    class Solution {

    public int reverse(int x) {
        StringBuilder sb = new StringBuilder(""+x);
        sb.reverse();
        String str = sb.toString();
        if(str.charAt(str.length()-1) == '-')
            str = "-" + str.substring(0, str.length()-1);
        int ans = 0;
        
        try{
            ans = Integer.parseInt(str);
        } catch(Exception ex){
            return 0;
        }
        
        return ans;
    }
    

    }
    '''


Log in to reply
 

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