LeetCode 12 Integer to Roman(C,C++,Java,Python)
阿新 • • 發佈:2019-02-10
Problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Solution:
根據數字將每一位轉換為羅馬字串即可,時間複雜度O(len(num))題目大意:
給一個整數,將整數調整為羅馬數字,關於羅馬數字的定義見這裡:羅馬數字- 個位數舉例 Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ,6】Ⅶ,7】 Ⅷ,8 】Ⅸ,9 】
- 十位數舉例 Ⅹ,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 】
- 百位數舉例 C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
- 千位數舉例 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】
- 千位數以上舉例
解題思路:
除了零位之外,把所有位轉化為羅馬數字連線即可Java原始碼(用時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語言原始碼(用時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++原始碼(用時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原始碼(用時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