Unordered_set dose not work?


  • 0
    M

    string fractionToDecimal(int numerator, int denominator) {
    if (numerator == INT_MIN && denominator == -1)
    return "2147483648";

    unordered_set<int> set;
    string strRet;
    
    int quotient = numerator / denominator;
    int remainder = numerator % denominator;
    
    stringstream ss;
    ss << quotient;
    strRet = ss.str();
    
    while (1)
    {
    	if (0 == remainder)
    	{
    		if (set.empty()) break;
    
    		stringstream ss;
    		for (auto iter = set.begin(); iter != set.end(); ++iter)
    			ss << *iter;
    
    		strRet += "." + ss.str();
    		break;
    	}
    	remainder *= 10;
    	int q = remainder / denominator;
    	remainder = remainder % denominator;
    	auto it = set.find(q);
    	if (it == set.end())
    	{
    		set.insert(q);
    	}
    	else
    	{
    		stringstream ss;
    		for (auto iter = set.begin(); iter != it; ++iter)
    			ss << *iter;
    
    		strRet += "." + ss.str();
    		
    		ss.str("");
    		for (auto iter = it; iter != set.end(); ++iter)
    			ss << *iter;
    
    		strRet += "(" + ss.str() + ")";
    		break;
    	}
    }
    
    return strRet;
    

    }

    Input: 1 6

    Your answer : 0.(61)

    Expected answer: 0.1(6)


Log in to reply
 

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