Leetcode--integer to Roman

Source: Internet
Author: User

Title Description:

Given an integer, convert it to a Roman numeral.

Input is guaranteed to being within the range from 1 to 3999.

Title Analysis: The Roman form of an shaping number is expressed. Roman numerals and shaping number conversion rules refer to http://blog.csdn.net/sinat_24520925/article/details/44560375. Roman Numerals are the oldest representation of numbers, more than 2000 years earlier than Arab arrays, originating from Rome
Roman numerals have the following symbols:
Basic characters I V X L C D M
corresponds to Arabic numerals 1 5 10 50 100 500 1000
count rule: The same number ligatures, the number represented is equal to the number added to the number, for example: III = 3 The small number to the right of the large number, the number represented is equal to the number of the sum of these numbers, for example: VIII = 8 small number, Limited (I, X and C) on the left of large numbers, The number represented is equal to the number of the large number minus the decimal, for example: IV = 4 in normal use, the continuous number of repetitions must not exceed three times, on a number of horizontal lines, indicating that the number is enlarged 1000 times times (the subject is only considered within 3999 of the number, so the rule is not used)
Secondly, the Roman numeral to Arabic numerals rule (limited to 3999):
Three algorithms are given below. Method One, first give the number corresponding to the Roman character form. According to the rule of counting, we can give 30 numbers corresponding to the string, 9 (1~9), 9 (10~90), 9 (100~900), 3 (1000~3000) after the given number of shaping N from high to low output its corresponding Roman characters. The specific code is as follows:
string inttoroman (int num) {string m[]={"", "M", "MM", "MMM"};string c[]={"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC" , "CM"};string x[]={"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};string i[]={"", "I", "II", "III", "IV", "V", "VI "," VII "," VIII "," IX "};return m[num/1000]+c[(num%1000)/100]+x[(num%100)/10]+i[(num%10)];}
Method Two
Becauseconsecutive digits cannot be repeated more than three times, so there is a special form when it is greater than three times. We do not have to list all 30 combinations, as long as the special form (13), the other forms can be obtained from these 13 combinations. Outputs the corresponding Roman character with the largest number of shaping numbers and subtracts the number of shapes corresponding to this Roman character. This is always judged until the single digit of the given number of shapes is output. The code is as follows:
string intToRoman2 (int num) {if (num>=1000) return "M" +inttoroman (num-1000), if (num>=900) return "CM" +inttoroman ( num-900), if (num>=500) return "D" +inttoroman (num-500), if (num>=400) return "CD" +inttoroman (num-400); if (num> =100) return "C" +inttoroman (num-100), if (num>=90) return "XC" +inttoroman (num-90), if (num>=50) return "L" + Inttoroman (NUM-50), if (num>=40) return "XL" +inttoroman (num-40), if (num>=10) return "X" +inttoroman (num-10); if ( num>=9) return "IX" +inttoroman (num-9), if (num>=5) return "V" +inttoroman (num-5), if (num>=4) return "IV" + Inttoroman (num-4); if (num>=1) return "I" +inttoroman (num-1); return "";}

Method Threethought same as method two, but the 13 Roman characters and their corresponding shaping corresponding to store in the array, the code is as follows;
string inttoroman (int num) {for       (int i=12;i>=0;i--) {if (inte[i]<=num) {return Roman[i]+inttoroman (num-inte[ I]);}} Return "";    }   Private:    int  inte[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};string roman[13]={"I", "IV", "V", "IX", "X "," XL "," L "," XC "," C "," CD "," D "," CM "," M "};

The first method is straightforward, but has a large spatial complexity and runs in Leetcode for a length of 96ms. The second method reduces the complexity of space and runs at 54ms. The third method further reduces the spatial complexity in the thought of method two, and the running time of Leetcode is 51ms.The complete code is as follows, where we use the Roman to integer function in http://blog.csdn.net/sinat_24520925/article/details/44560375 to determineRoman to Integerthe correctness of three kinds of algorithms.
#include <iostream> #include <string> #include <vector>using namespace Std;int romantoint (string s); string inttoroman (int num), string intToRoman1 (int num), string intToRoman2 (int num), void Main () {int n=2549;cout<< N<<endl;string Res=inttoroman (n); Cout<<res<<endl;cout<<romantoint (res) <<endl; String Res1=inttoroman1 (n); Cout<<res1<<endl;cout<<romantoint (res1) <<endl; String res2=inttoroman2 (n); Cout<<res2<<endl;cout<<romantoint (Res2); }string inttoroman (int num) {string m[]={"", "M", "MM", "MMM"};string c[]={"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC "," CM "};string x[]={" "," X "," XX "," XXX "," XL "," L "," LX "," LXX "," LXXX "," XC "};string i[]={" "," I "," II "," III "," IV "," V "," VI "," VII "," VIII "," IX "};return m[num/1000]+c[(num%1000)/100]+x[(num%100)/10]+i[(num%10)];} int inte[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000};string roman[13]={"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C "," CD "," D "," CM "," M "};string intToRoman1 (int num) {for (int i=12;i>=0;i--) {if (inte[i]<=num) {return Roman[i]+inttoroman (Num-inte[i]);}} Return "";} string intToRoman2 (int num) {if (num>=1000) return "M" +inttoroman (num-1000), if (num>=900) return "CM" +inttoroman ( num-900), if (num>=500) return "D" +inttoroman (num-500), if (num>=400) return "CD" +inttoroman (num-400); if (num> =100) return "C" +inttoroman (num-100), if (num>=90) return "XC" +inttoroman (num-90), if (num>=50) return "L" + Inttoroman (NUM-50), if (num>=40) return "XL" +inttoroman (num-40), if (num>=10) return "X" +inttoroman (num-10); if ( num>=9) return "IX" +inttoroman (num-9), if (num>=5) return "V" +inttoroman (num-5), if (num>=4) return "IV" + Inttoroman (num-4); if (num>=1) return "I" +inttoroman (num-1); return "";}       int Romantoint (string s) {/*roman[' I ']=1;       roman[' V ']=5;       roman[' X ']=10;       roman[' L ']=50;       roman[' C ']=100;       roman[' D ']=500;  roman[' M ']=1000;        */char *p=new char[s.size ()]; strcpy (P,s.c_str ());//Convert string type to char array vector<int> array;      Array.resize (S.size ());      int n=0;          for (int i=0;i<s.size (), i++) {switch (P[i]) {case ' I ': array[i]=1;break;          Case ' V ': array[i]=5;break;          Case ' X ': array[i]=10;break;          Case ' L ': array[i]=50;break;          Case ' C ': array[i]=100;break;          Case ' D ': array[i]=500;break;          Case ' M ': array[i]=1000;break;          default:cout<< "error\n";          } if (i==0) {n+=array[i];              } else {if (array[i]<=array[i-1]) {n+=array[i];              } else {n+=array[i]-2*array[i-1];  }}} return n;   }

Leetcode--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.