My accepted code for C++


  • 0
    8

    There is a solution using "stoll". I use s.rbegin() and s.rend() to reverse the integer.
    Code is as follows:

    int si = x > 0 ? 1 : -1;
    	stringstream ss;
    	ss << x;
    	string s = ss.str();
    	s =	s.substr(s.find_first_of("0123456789"));
    	string s2(s.rbegin(),s.rend());
    	long long y = stoll(s2,NULL,10);
    	y = y*si;
    	if (y > INT_MAX ||y<INT_MIN)
    	       y = 0;
    	return y;
    

Log in to reply
 

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