My weird 8ms C++ code use string


  • 0
    Y
    class Solution {
    public:
        int reverse(int x) {
            int re;
            string s = to_string(x);
            auto l = s.begin();
            auto r = prev(s.end());
            if(*l=='-')l++;
            while(l<r){
                char temp = *l;
                *l = *r;
                *r = temp;
                l++;
                r--;
            }
            try{
              re =  stoi(s);
            }catch(exception e){
                re = 0;
            }
            return re;
        }
    };

Log in to reply
 

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