A 9ms new method


  • 0
    F

    Using string to help.

    #include <stringstream>
    
    using namespace std;
    class Solution {
    public:
    	int reverse(int x) {
    		string str;
    		std::stringstream ss;  
    		ss << x;  
    		ss >> str;
    
            char temp;
    		for (int i = (x<0?1:0), j = str.length()-1; i < j; i++,j--) {
    		    temp = str[i];
    		    str[i] = str[j];
    		    str[j] = temp;
    		}
    		
    	    long l = atol(str.c_str());
    		if (l >= INT_MIN && l <= INT_MAX)
    			return (int)l;
    
    		return 0;
    	}
    };
    

Log in to reply
 

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