Getting time limit exceeded for num=1000, but it runs in 4ms on my machine. Can anyone help?


  • 0
    S
    string intToRoman(int num) {
    unordered_map<int, string> mp { {1,"I"},{4,"IV"}, {5,"V"},{9,"IX"}, {10,"X"}, {40,"XL"},{50,"L"}, {90,"XC"},
    	{100,"C"}, {400,"CD"},{500,"D"}, {900,"CM"},{1000,"M"} };
    int a[]={1,4,5,9,10,40,50,90,100,400,500,900,1000};
    int i,diff;
    string str="";
    i=0;
    while(num != 0)
    {
    	//cout<<"num="<<num<<endl;
    	i=0;
    	while(num>=a[i])
    	i++;
    
    	str=str+mp[a[i-1]];
    	num=num-a[i-1];
    
    
    }
    return str;
    }

Log in to reply
 

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