MENU

LeetCode7. 反转整数

July 8, 2018 • Read: 267 • LeetCode

image
一位一位取出x,最后判断是否超出int的范围即可

class Solution {
    public int reverse(int x) {
        long ans = 0;
        int MIN_INT = 0x80000000;
        int MAX_INT = 0x7fffffff;
        while(x != 0) {
            ans = ans * 10 + (x % 10);
            x /= 10;
        }
        if(ans < MIN_INT || ans > MAX_INT) 
            ans = 0;
        return (int)ans;
    }
}
最后编辑于: October 10, 2018
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment