[12]Integer to Roman
来源:互联网 发布:淘宝珍珠内裤买家晒图 编辑:程序博客网 时间:2024/04/28 19:27
【题目描述】
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
【思路】
罗马数字记数方法
基本字符:
I、V、X、L、C、D、M
相应的阿拉伯数字表示为:
1、5、10、50、100、500、1000
(1)相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
(2)小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
(3)小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
(4)正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
(5)在一个数的上面画一条横线,表示这个数扩大1000倍。
组数规则
(1)基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个。
(2)不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目,只能使用一个。
(3)V 和X 左边的小数字只能用Ⅰ。
(4)L 和C 左边的小数字只能用X。
(5)D 和M 左边的小数字只能用C。
【代码】
32ms:
class Solution {public: string intToRoman(int num) { string ans=""; char roman[7]={'I', 'V', 'X', 'L', 'C', 'D', 'M'}; int pos=0; while(num>0){ int tmp=num%10; switch(tmp){ case 3: { ans=roman[pos]+ans; tmp--; } case 2: { ans=roman[pos]+ans; tmp--; } case 1: { ans=roman[pos]+ans; break; } case 4: { ans=roman[pos+1]+ans; ans=roman[pos]+ans; break; } case 8: { ans=roman[pos]+ans; tmp--; } case 7: { ans=roman[pos]+ans; tmp--; } case 6: { ans=roman[pos]+ans; tmp--; } case 5: { ans=roman[pos+1]+ans; break; } case 9: { ans=roman[pos+2]+ans; ans=roman[pos]+ans; break; } default: {break;} } pos+=2; num=num/10; } return ans; }};
40ms:时间长但代码简洁很多
class Solution {public: 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%1000)%100)/10]+I[num%10]; }};
28ms:
class Solution {public: string intToRoman(int num) { int s[7]={0}; int integer[7]={1000,500,100,50,10,5,1}; char roman[7]={'M','D','C','L','X','V','I'}; string ans; int index=0; while(index<7){ s[index]=num/integer[index]; num=num%integer[index]; index++; } for(int i=0;i<7;i++){ if(s[i]==1&&i+1<7&&s[i+1]==4) continue; if(s[i]==4){ if(s[i-1]==1) ans=ans+roman[i]+roman[i-2]; else ans=ans+roman[i]+roman[i-1]; } else{ for(int j=0;j<s[i];j++){ ans+=roman[i]; } } } return ans; }};
0 0
- 12、Integer to Roman
- 12、Integer to Roman
- 12Integer to Roman
- #12 Integer to Roman
- 12 Integer to Roman
- [12]Integer to Roman
- 12、Integer to Roman
- 【12】Integer to Roman
- 12 Integer to Roman
- 12-Integer to Roman
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- LeetCode_OJ【12】【13】Integer to Roman && Roman to Integer
- <LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
- leetcode 12|13. Integer to Roman && Roman to Integer
- [LeetCode 12] Integer to Roman
- leetcode 12 Integer to Roman
- [leetcode 12] Integer to Roman
- [Leetcode] 12 - Integer to Roman
- Boost源码学习二[内存管理一]上
- 关于Flash的几点思考(Thoughts on Flash)
- MVN 相关命令
- linux shell(一)
- 小猪慢跑
- [12]Integer to Roman
- 【五校联考7day2】集体照
- BONECP学习笔记
- SharePoint 2010的Document Library Upload Multiple Documents功能失效的解决方案
- 开机NTP服务器报错ntpd_intres++ntp_intres.request:+permission+denied
- Android截图包含webview时,webview部分空白(其他部分正常),此问题发生在系统4.4及以上
- 常用正则表达式
- 以继承基类的功能!
- java上传excel并读取excel