reverse with vector


  • 0
    G
    class Solution {
    public:
        int reverse(int x) {
            if(x == 0) return x;
            long t,answer=0;
            vector<long> l;
            while(x)
            {
                t = x%10;
                l.push_back(t);
                x /= 10;
            }
            vector<long>::iterator it;
    
            for(it = l.begin(); it<=l.end()-1; it++){
                answer = *it + answer*10;
            }
            if(answer > (long)pow(2,31) || answer < -(long)pow(2,31)) return 0;
            return answer;
        }
    };
    

Log in to reply
 

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