数字反转(力扣题库)

    public static void main(String[] args) {
        System.out.println(a(-123));
    }
    public static int a(int i){
try {
    StringBuffer stringBuffer = new StringBuffer(i + "");
    if (stringBuffer.indexOf("-") == -1) {//如果不是负数
        stringBuffer.reverse();
        return Integer.parseInt(stringBuffer.toString());
    }
    String str = stringBuffer.substring(stringBuffer.indexOf("-") + 1, stringBuffer.length());
    StringBuffer buffer = new StringBuffer(str);
    buffer.reverse();
    return Integer.parseInt("-" + buffer.toString());
}catch (Exception e){
    return 0;
}

    }

你可能感兴趣的:(java算法)