Pass 34/35 tests except: Input: -2147483648, 1


  • 0
    U

    the last test output was wrong :
    Input: -2147483648, 1

    why? can anyone give a solution?

    The code :

    string fractionToDecimal(int numerator, int denominator)
    {
    bool negtive = false;

    negtive = (numerator<0 && denominator>0) || (numerator>0 && denominator<0);
        
    unordered_map<int, int> occur;
    
    string str = "";
    
    long numer = numerator;
    long denom = denominator;
    numer = abs(numer);
    denom = abs(denom);
    
    long num = numer/denom;
    stringstream ss;
    ss<<num;
    
    str = ss.str();
    
    num = numer%denom;
    
    if(num==0)
    	return str;
    
    str = str+'.';
    
    string after="";
    vector<char> afternum;
    auto iter = occur.end();
    int i=0;
    int pos = -1;
    do{
    	iter = occur.find(num);
    
    	if (iter != occur.end())
    	{
    		pos = occur[num];
    
    
    		break;
    	}
    
    	int x= (num*10)/denom;
    	char c= (x&0x00ff)+'0';
    
    	afternum.push_back(c);
    	occur[num]= i;
    	num = (num*10)%denom;
    	i++;
    }while(num!=0);
    
    if(pos !=-1)
    {
    	for(int i=0;i< occur[num];i++)
    	{
    		after += afternum[i]; 
    	}
    
    	after += '(';
    
    	for(int i=occur[num]; i< afternum.size(); i++)
    	{
    		after += afternum[i];
    	}
    
    	after += ')';
    }
    else
    {
    	for(int i=0;i< afternum.size();i++)
    		after += afternum[i]; 
    
    }
    
    if(negtive)
    	str = '-'+str+after;
    else
    	str = str+after;
    
    return str;
    

    }


  • 0
    T

    I have this problem too. I may be caused by overflow.


Log in to reply
 

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