Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
640 B
0
Indexable
Never
class Solution {
public:
    int reverse(int x) {
        bool neg=false;
        if(x<0){
            neg=true;
            x=abs(x);
        }
        long res=0,dig=0,l=x;
        while(l>0) {
            l=l/10;
            dig++;
        }
        dig--;

        while(x>0){
            if(res>INT_MAX || res<INT_MIN) return 0;
            res+=pow(10,dig)*(x%10);
            x=x/10;
            dig--;
        }
        int resf=0;
        if(neg) resf=-res;
        else resf=res;
        return resf;
    }
};

/* 
int l=x;
        while(l>0) {
            l=l/10;
            dig++;
        }
        dig--;
        int res=0; */
Leave a Comment