Reverse Integer c++ solution


  • 0
    Y

    class Solution {
    public:
    int reverse(int x) {
    int tag=0;
    int result;
    long long ret=1;
    vector<int> temp;

        if(x<0){
            tag=1;
            x=-x;
        }
        else if(x==0){
            return 0;
        }
        while(x!=0){
            result=x%10;
            x=x/10;
            temp.push_back(result);
        }
        
        ret=temp[0];
        
        for(vector<int>::iterator it=temp.begin()+1;it<temp.end();it++){
            ret=ret*10;
            ret=ret+*it;
        }
        
        if(tag==1){
            ret=-ret;
        }
        
        if(ret>INT_MAX or ret<INT_MIN)
            return 0;
        return ret;
        
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.