当前位置:网站首页>[leetcode] 92 integer inversion

[leetcode] 92 integer inversion

2020-11-09 23:48:13 JaneRoad

subject :

image-20201109230724251

image-20201109230738067

Their thinking :

Pop and push numbers & Check before spillage

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

Code :

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]所创,转载请带上原文链接,感谢