7. Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
Solution:
public class Solution {
    public int reverse(int x) {
        int sign = x<0? -1:1;
        x= Math.abs(x);
        StringBuilder rt = new StringBuilder(String.valueOf(x));
        rt.reverse();
        try{
            return Integer.parseInt(rt.toString())*sign;
        }
        catch(Exception e){
            return 0;
        }

    }
}

results matching ""

    No results matching ""