当前位置:网站首页>【LeetCode】12、整数转罗马数字
【LeetCode】12、整数转罗马数字
2022-06-28 05:05:00 【小曲同学呀】
12、整数转罗马数字
题目:
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给你一个整数,将其转为罗马数字。
示例1:
输入: num = 3
输出: "III"
示例 2:
输入: num = 4
输出: "IV"
示例 3:
输入: num = 9
输出: "IX"
示例 4:
输入: num = 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.
示例 5:
输入: num = 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
解题思路:
此题的解题思路,比较多,比如贪心算法,暴力拆解,枚举。拼接等等。
咱们就用拼接法来实现一下:
参考代码:
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();
}
}

边栏推荐
- Analysis of distributed transaction TCC
- 信息学奥赛一本通 1360:奇怪的电梯(lift)
- Have you finished the examination of level II cost engineer? There are also qualification regulations!
- Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
- Biovendor sRAGE antibody solution
- Excel将一行的内容进行复制时,列与列之间是用制表符“\t”进行分隔的
- Differences between pragma and ifndef
- 大促场景下,如何做好网关高可用防护
- Unity out ref params
- 交流电和直流电的区别是什么?
猜你喜欢

2022 high altitude installation, maintenance and removal examination questions and answers

并发之wait/notify说明

How to do a good job of gateway high availability protection in the big promotion scenario

Leetcode 88: merge two ordered arrays

Study on modified triphosphate: lumiprobe amino-11-ddutp

Don't roll! How to reproduce a paper with high quality?

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

Latest Windows version 5.0.14 of redis

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

Where does the storm go? Whose pot is the weather forecast wrong?
随机推荐
PHP code wechat, official account and enterprise wechat send emoticons [u+1f449]
It is the latest weapon to cross the blockade. It is one of the fastest ladders.
Carboxylic acid study: lumiprobe sulfoacyanine 7 dicarboxylic acid
CPG 固体支持物研究:Lumiprobe通用 CPG II 型
Sword finger offer 47 Maximum gift value (DP)
Role of native keyword
Standard particle swarm optimization C language program
Simple usage of GSAP
PCR/qPCR研究:Lumiprobe丨dsGreen 用于实时 PCR
汇编常用指令
二级造价工程师考试还没完?还有资格审核规定!
QCOM LCD调试
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
Leetcode 88: merge two ordered arrays
Reactive dye research: lumiprobe af594 NHS ester, 5-isomer
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
2022 safety officer-b certificate examination question bank and answers
How high is the gold content of grade II cost engineer certificate? Just look at this
学习太极创客 — MQTT 第二章(四)ESP8266 保留消息应用