Leetcode 反转数组

Java
class Solution {
    public int reverse(int x) {
        long num=0;
        while(x!=0){
            num=num*10+x%10;
            x=x/10;
        }
        if(num>=Integer.MAX_VALUE||num<=Integer.MIN_VALUE){
            return 0;
        }
        return (int)num;
    }
}

C++
class Solution {
public:
    int reverse(int x) {
        bool flag=true;
        if(x<0){
            flag=false;
            x=-x;
        }
        int num=0;
        while(x){
            if ((num != 0 && (INT_MAX / num < 10)) )
                return 0;
            num=num*10+x%10;
            x/=10;
        }
        if(!flag) num=-num;
        return num;
    }
};

你可能感兴趣的:(code,leetcode)