当前位置:网站首页>【LeetCode】 92 整数反转

【LeetCode】 92 整数反转

2020-11-09 23:48:13 JaneRoad

题目:

image-20201109230724251

image-20201109230738067

解题思路:

弹出和推入数字 & 溢出前进行检查

https://leetcode-cn.com/problems/reverse-integer/solution/zheng-shu-fan-zhuan-by-leetcode/

代码:

package com.janeroad;

/**
* Created on 2020/11/9.
*
* [@author](https://my.oschina.net/arthor) LJN
*/
public class LC142 {
public static void main(String[] args) {
    System.out.println(reverse(1999));
}
public static int reverse(int x) {
    int rev = 0;
    while (x != 0) {
        int pop = x % 10;
        x /= 10;
        if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
        if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
        rev = rev * 10 + pop;
    }
    return rev;
}


}

版权声明
本文为[JaneRoad]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4248053/blog/4710382