Integer to Roman

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.