当前位置:网站首页>[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();
}
}

边栏推荐
- 学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用
- metaRTC5.0 API编程指南(一)
- 穿越封锁的最新利器,速度最快梯没有之一。
- Where does the storm go? Whose pot is the weather forecast wrong?
- Severe tire damage: the first rock band in the world to broadcast live on the Internet
- Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
- Feign remote call fallback callback failed, no effect
- IP datagram sending and forwarding process
- Blocking, non blocking, IO multiplexing select\poll\epoll
- 【JVM系列】JVM调优
猜你喜欢

2022新版nft源码中国元宇宙数字藏品艺术品交易平台源码

Reactive dye research: lumiprobe af594 NHS ester, 5-isomer

The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022

Performance optimization and implementation of video codec

Binary sort tree: BST

JS text box loses focus to modify width text and symbols

cgo+gSoap+onvif学习总结:8、arm平台交叉编译运行及常见问题总结

A doctor's 22 years in Huawei (full of dry goods)

wordpress zibll子比主题6.4.1开心版 免授权

Light collector, Yunnan Baiyao!
随机推荐
【JVM系列】JVM调优
【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
2022年材料员-通用基础(材料员)操作证考试题库及答案
Severe tire damage: the first rock band in the world to broadcast live on the Internet
Generate QR code in wechat applet
短视频本地生活版块成为热门,如何把握新的风口机遇?
分享一个因子挖掘的利器:遗传规划
How to do a good job of gateway high availability protection in the big promotion scenario
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
QCOM LCD调试
sqlmap工具使用手册
Project practice! Teach you JMeter performance test hand in hand
Study on chemical properties and technology of biovendor rage ELISA Kit
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
How long will the PMP test results come out? You must know this!
开关电源电压型与电流型控制
使用class toplevel的messagebox时,窗口弹出问题。
高通平台 Camera 之 MCLK 配置
[skywalking] learn distributed link tracking skywalking at one go
别卷!如何高质量地复现一篇论文?