Leetcode Integer to Roman (C,c++,java,python)

Source: Internet
Author: User

Problem:

Given an integer, convert it to a Roman numeral.

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

Solution: Each bit is converted to a Roman string according to the number, and time complexity O (NUM) is the main topic: Give an integer, adjust the integer to Roman numerals, and the definition of Roman numerals here: Roman numerals
  • Single Digit example ⅰ,1 "ⅱ,2" ⅲ,3 "ⅳ,4" ⅴ,5 "ⅵ,6" ⅶ,7 "ⅷ,8" ⅸ,9 "
  • 10-digit example ⅹ,10 "ⅺ,11" ⅻ,12 "xiii,13" xiv,14 "xv,15" xvi,16 "xvii,17" xviii,18 "xix,19" xx,20 "xxi,21" xxii,22 "XXIX,29 "xxx,30" xxxiv,34 "xxxv,35" xxxix,39 "xl,40" l,50 "li,51" lv,55 "lx,60" lxv,65 "lxxx,80" xc,90 "xciii,93" XCV,95 "XCVIII, 98 "xcix,99"
  • examples of hundred c,100 "cc,200" ccc,300 "cd,400" d,500 "dc,600" dcc,700 "dccc,800" cm,900 "cmxcix,999"
  • Thousands of examples m,1000 "mc,1100" mcd,1400 "md,1500" mdc,1600 "mdclxvi,1666" mdccclxxxviii,1888 "mdcccxcix,1899" MCM,1900 "MCMLXXVI,1976 "mcmlxxxiv,1984" mcmxc,1990 "mm,2000" mmmcmxcix,3999 "
  • More than thousands of examples
, 65,259 ", 134945584", 183650 "

Solution: Except 0 bits, convert all the bits to a Roman digital connection
Java source code (spents 305ms):
public class Solution {public String inttoroman (int num) {StringBuilder sb = new StringBuilder ();            if (num/1000!=0) {romandigit (sb,num/1000, "M", "#", "#");        num%=1000;            } if (num/100!=0) {romandigit (sb,num/100, "C", "D", "M");        num%=100;            } if (num/10!=0) {romandigit (SB,NUM/10, "X", "L", "C");        num%=10;        } if (num!=0) {romandigit (Sb,num, "I", "V", "X");    } return new String (SB);  } private void Romandigit (StringBuilder sb,int digit,string a,string b,string c) {switch (digit) {case            1:sb.append (a); return;            Case 2:sb.append (a+a); return;            Case 3:sb.append (a+a+a); return;            Case 4:sb.append (a+b); return;            Case 5:sb.append (b); return;            Case 6:sb.append (b+a); return;            Case 7:sb.append (b+a+a); return;            Case 8:sb.append (b+a+a+a); return;       Case 9:sb.append (a+c); return; } return; }}

C Language Source code (spents 16ms):
int Romandigit (char* roman,int digit,char A,char b,char c) {switch (digit) {case 1:roman[0]=a;return 1;        Case 2:roman[0]=a;roman[1]=a;return 2;        Case 3:roman[0]=a;roman[1]=a;roman[2]=a;return 3;        Case 4:roman[0]=a;roman[1]=b;return 2;        Case 5:roman[0]=b;return 1;        Case 6:roman[0]=b;roman[1]=a;return 2;        Case 7:roman[0]=b;roman[1]=a;roman[2]=a;return 3;        Case 8:roman[0]=b;roman[1]=a;roman[2]=a;roman[3]=a;return 4;    Case 9:roman[0]=a;roman[1]=c;return 2; } return 0;}    char* inttoroman (int num) {//char Digit[9][5]={i,ii,iii,iv,v,vi,vii,viii,ix};    Char TENSDIG[9][5]={X,XX,XXX,XL,L,LX,LXX,LXXX,XC};    Char hunsdig[9][5]={c,cc,ccc,cd,d,dc,dcc,dccc,cm};    Char thodig[3][3]={m,mm,mmm};    char* roman= (char*) malloc (sizeof (char) *16);    int index=0;        if (num/1000!=0) {index+=romandigit (roman+index,num/1000, ' M ', ' # ', ' # ');    num%=1000; } if (num/100!=0) {index+=romandigit (roman+index,num/100, ' C ', ' D ', 'M ');    num%=100;        } if (num/10!=0) {index+=romandigit (ROMAN+INDEX,NUM/10, ' X ', ' L ', ' C ');    num%=10;    } if (num!=0) {index+=romandigit (roman+index,num, ' I ', ' V ', ' X ');    } roman[index]=0; return Roman;}
C + + source code (spents 50ms):
Class Solution {public:string inttoroman (int num) {char* roman= (char*) malloc (sizeof (char) *16);        int index=0;            if (num/1000) {index+=romandigit (roman+index,num/1000, ' M ', ' # ', ' # ');        num%=1000;            } if (num/100) {index+=romandigit (roman+index,num/100, ' C ', ' D ', ' M ');        num%=100;            } if (NUM/10) {index+=romandigit (ROMAN+INDEX,NUM/10, ' X ', ' L ', ' C ');        num%=10;        } if (num) {index+=romandigit (roman+index,num, ' I ', ' V ', ' X ');        } roman[index]=0;    return string (roman);  }private:int romandigit (char* s,int digit,char A,char b,char c) {switch (digit) {case 1:s[0]=a;return            1;            Case 2:s[0]=a;s[1]=a;return 2;            Case 3:s[0]=a;s[1]=a;s[2]=a;return 3;            Case 4:s[0]=a;s[1]=b;return 2;            Case 5:s[0]=b;return 1;            Case 6:s[0]=b;s[1]=a;return 2;           Case 7:s[0]=b;s[1]=a;s[2]=a;return 3; Case 8:s[0]=b;s[1]=a;s[2]=a;s[3]=a;return 4;        Case 9:s[0]=a;s[1]=c;return 2;    } return 0; }};
Python source code (spents 283ms):
Class solution:    # @param {integer} num    # @return {string}    def inttoroman (self, num):        s= "        if num/ 1000!=0:            s=self. Romandigit (s,num/1000, ' M ', ' # ', ' # ')            num%=1000        if num/100!=0:            s=self. Romandigit (s,num/100, ' C ', ' D ', ' M ')            num%=100        if num/10!=0:            s=self. Romandigit (S,NUM/10, ' X ', ' L ', ' C ')            num%=10        if num!=0:            s=self. Romandigit (S,num, ' I ', ' V ', ' X ')        return s    def romandigit (self,s,digit,a,b,c):        if digit<4:            s+= A*digit            return s        elif digit==4:            s+=a+b            return s        elif digit<9:            s+=b+a* (digit-5)            return s        else:            s+=a+c            return s


Leetcode Integer to Roman (C,c++,java,python)

Related Article

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.