C++ implementation: using vector


  • 0
    T
    class Solution {
    public:
        int reverse(int x) {
            vector<int> array;
            int y = x;
            int sign = 1;
            if(y<0)
            {
                y = -y;
                sign = -1;
            }
            while(y>0)
            {
                array.push_back(y%10);
                y = y/10;
            }
            
            long long int value = 0;
            int n = array.size();
            int i;
            for(i=0; i<n; i++)
            {
                value = value*10 + array[i];
            }
            
            if(value > INT_MAX)
                return 0;
            else 
                return (int) value*sign;
        }
    };

Log in to reply
 

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