数字反转(力扣题库)

    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;
}

    }

猜你喜欢

转载自blog.csdn.net/qq_42903710/article/details/88537262