7 line solution


  • 0
    H
    class Solution {
    public:
        int reverse(int x) {
            vector<int> myvector;
            int y = (x > 0) ? (x) : (-x);
            for( ; y > 0; y = y / 10) myvector.push_back(y % 10);
            for(y = 0; !myvector.empty(); myvector.erase(myvector.begin())){
                if((y * 10 / 10 != y)) return 0;
                y = y * 10 + myvector[0];
            }
            return (x > 0) ? (y) : (-y);
        }
    };
    

Log in to reply
 

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