当前位置:网站首页>[leetcode] 12. Integer to Roman numeral
[leetcode] 12. Integer to Roman numeral
2022-06-28 05:06:00 【Xiaoqu】
12、 Integer to Roman number
subject :
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1.12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
C Can be placed in D (500) and M (1000) Left side , To express 400 and 900.
Give you an integer , Turn it into Roman numerals .
Example 1:
Input : num = 3
Output : "III"
Example 2:
Input : num = 4
Output : "IV"
Example 3:
Input : num = 9
Output : "IX"
Example 4:
Input : num = 58
Output : "LVIII"
explain : L = 50, V = 5, III = 3.
Example 5:
Input : num = 1994
Output : "MCMXCIV"
explain : M = 1000, CM = 900, XC = 90, IV = 4.
Their thinking :
Ideas for solving this problem , More , For example, greedy algorithm , Violence , enumeration . Splicing and so on .
Let's use the splicing method to realize :
Reference code :
class Solution {
public String intToRoman(int num) {
int[] values={
1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] rom={
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
StringBuilder sb=new StringBuilder();
for(int i=0;i<values.length;i++){
while(num>=values[i]){
sb.append(rom[i]);
num-=values[i];
}
}
return sb.toString();
}
}

边栏推荐
- Principle and implementation of SSD for target detection
- Unity delegate
- 程序员-放羊娃
- RxSwift --(1)创建一个项目
- BioVendor sRAGE Elisa试剂盒化学性质和技术研究
- 判断对象中是否存在某一个属性
- mysql----where 1=1是什么意思
- !‘ Cat 'is not an internal or external command, nor is it a runnable program or batch file.
- wordpress zibll子比主题6.4.1开心版 免授权
- 【SkyWalking】一口气学完分布式链路追踪SkyWalking
猜你喜欢

A guide to P2P network penetration (stun) for metartc5.0 programming

Performance optimization and implementation of video codec

Severe tire damage: the first rock band in the world to broadcast live on the Internet

分享一个因子挖掘的利器:遗传规划

Light collector, Yunnan Baiyao!

基于订单流工具,我们能看到什么?

机器人学DH参数及利用matlab符号运算推导

Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid

quartus 复制IP核

Study on modified triphosphate: lumiprobe amino-11-ddutp
随机推荐
穿越封锁的最新利器,速度最快梯没有之一。
几百行代码实现一个脚本解释器
[NOIP2002 普及组] 过河卒
Role of native keyword
A guide to P2P network penetration (stun) for metartc5.0 programming
并发之wait/notify说明
Assembly common instructions
2022 safety officer-b certificate examination question bank and answers
The number of small stores in Suning has dropped sharply by 428 in one year. Zhangkangyang, the son of Zhang Jindong, is the actual controller
cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结
Hundreds of lines of code to implement a script interpreter
Idle interrupt cannot be cleared
通过例子学习Rust
lotus v1.16.0 calibnet
Opencv实现颜色检测
Dart learning - functions, classes
sqlmap工具使用手册
JS 文本框失去焦点修改全半角文字和符号
2022西式面点师(高级)考试试题模拟考试平台操作
It is the latest weapon to cross the blockade. It is one of the fastest ladders.