Simple C++ Solution Using Exception Handling


#1
int Solution::reverse(int A) {
    int check =0;
    if(A<0) {
        check = 1;
        A = -A;
    }
    string str = "";
    str += to_string(A);
    std::reverse(str.begin(),str.end());
    int res = 0;
    try{
        res = stoi(str);
    }
    catch(...){
        return 0;
    }
    if(check)
        res = -res;
    return res;
}