I did not think that the hash table insert is so poor performance, the hash tables after the array, the running time is reduced by 100ms
Class Solution {Public:typedef pair<int,string> Sdata; string inttoroman (int num) {Sdata pattern[] = {pair<int,string> (+), "M"), Pair<int,string> ("CM"), pair<int,string> ("D"), Pair<int,string> ("CD"), pair<int,string> ("C"), Pair<int,string> ("XC"), pair<int,string> ("L"), Pair<int,string> ("XL"), Pair<int,string> (Ten, "X"), Pair<int,string> (9, "IX"), pair<int,string> (5, "V"), p Air<int,string> (4, "IV"), pair<int,string> (1, "I"),}; String str[]={"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; Unordered_map<int,string> buf; Cout<<sizeof (Sdata) <<endl; Cout<<sizeof (pattern[0]) <<endl; for (int i=0; i<sizeof (pattern)/sizeof (pattern[0]); i++)//buf.insert (Pattern[i]); string result; int index = 0; for (int i=0; i<sizeof (pattern)/sizeof (pattern[0]), i++) {index = Num/pattern[i].first;num = Num%pattern[i].first;int j = hashfunction (Pattern[i].first), while (index >0) {result +=str[j]; index--;} } return result; }int hashfunction (int num) {int index = 0;int result = 0;if (num >=100) {index = 2; num = num/100;} else if (num >=10) {index = 1; num = NUM/10;} Switch (num) {case 10:result = 12; Break Case 9:result = index*4 + 3;break; Case 5:result = index*4 + 2;break;case 4:result = index*4 + 1; Break;case 1:result = index*4 + 0; Break;default:result = 0;break;} return result;};
Integer to Roman