MENU

LeetCode7. 反转整数

July 8, 2018 • Read: 2911 • LeetCode阅读设置


一位一位取出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;
    }
}
Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment