当前位置:网站首页>[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();
}
}
边栏推荐
- A guide to P2P network penetration (stun) for metartc5.0 programming
- [noip2002 popularization group] cross the river pawn
- Simple usage of GSAP
- 公司为什么选择云数据库?它的魅力到底是什么!
- How to do a good job of gateway high availability protection in the big promotion scenario
- 吴恩达深度学习测验题:deeplearning.ai-week1-quiz
- 店铺进销存管理系统源码
- Understanding the source of innovation II
- 改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
- 2022电力电缆判断题模拟考试平台操作
猜你喜欢
Binary sort tree: BST
2022 high altitude installation, maintenance and removal examination questions and answers
Dart learning - functions, classes
!‘cat‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
metaRTC5.0编程之p2p网络穿透(stun)指南
活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
Severe tire damage: the first rock band in the world to broadcast live on the Internet
短视频本地生活版块成为热门,如何把握新的风口机遇?
DH parameters of robotics and derivation using MATLAB symbolic operation
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
随机推荐
109. simple chat room 12: realize client-side one-to-one chat
穿越封锁的最新利器,速度最快梯没有之一。
公司为什么选择云数据库?它的魅力到底是什么!
Analysis of distributed transaction solution Seata golang
JS text box loses focus to modify width text and symbols
Latest Windows version 5.0.14 of redis
Have you finished the examination of level II cost engineer? There are also qualification regulations!
IP datagram sending and forwarding process
羧酸研究:Lumiprobe 磺基花青7二羧酸
Interview: what are the similarities and differences between abstract classes and interfaces?
禁用右击、键盘打开控制台事件
【牛客网刷题系列 之 Verilog快速入门】~ 四选一多路器
Binary sort tree: BST
Biovendor sRAGE antibody solution
Learning Tai Chi Maker - mqtt Chapter II (VI) mqtt wills
Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
深度强化学习笔记
Distributed transaction - Final consistency scheme based on message compensation (local message table, message queue)
Unity delegate
Severe tire damage: the first rock band in the world to broadcast live on the Internet