码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:整数反转(Reserve Integer)

时间:2018-12-01 22:11:42      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:min   reserve   max   res   style   eve   结果   for   main   

public class ReserveInteger {
    public int reverse(int x) {
        //用于接收个位数(10的余数)
        int remainder;
        //是否负数
        int isMinus = 0;
        //存储结果
        double result = 0;
        //获得整数长度
        int length = ("" + x).length();
        if (x < 0) {
            isMinus = -1;
        }
        //遍历整数的每一位,如果是负数就少遍历一次
        for (int i = 0; i < length + isMinus; i++) {
            //取得个位数(带符号)
            remainder = x % 10;
            //取得反转数
            result = result * 10 + remainder;
            //去掉个位数
            x /= 10;
        }
        //判断是否值范围,返回反转数
        return (int) (result <= Integer.MAX_VALUE && result >= Integer.MIN_VALUE ? result : 0);
    }
}

 

LeetCode:整数反转(Reserve Integer)

标签:min   reserve   max   res   style   eve   结果   for   main   

原文地址:https://www.cnblogs.com/wymc/p/10050729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!